Skip to content

Commit

Permalink
[FIX] : add some extended number format like strtod
Browse files Browse the repository at this point in the history
  • Loading branch information
aiekick committed Jul 7, 2024
1 parent 1eab5e2 commit 64a3d01
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ImGuiFileDialog
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ SetTest(Test_IGFD_Utils_IsAValidCharForADigit_0)

SetTest(Test_IGFD_Utils_ExtractNumFromStringAtPos_0)
SetTest(Test_IGFD_Utils_ExtractNumFromStringAtPos_1)
SetTest(Test_IGFD_Utils_ExtractNumFromStringAtPos_2)

SetTest(Test_IGFD_Utils_NaturalCompare_0)
SetTest(Test_IGFD_Utils_NaturalCompare_1)
Expand Down
47 changes: 40 additions & 7 deletions tests/ImGuiFileDialog/Test_Utils.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "Test_FileStyle.h"

#include <cassert>
#include <cmath>

#include "ImGuiFileDialog/ImGuiFileDialog.h"
#include <ImGuiFileDialog/ImGuiFileDialog.h>

////////////////////////////////////////////////////////////////////////////
//// ReplaceString /////////////////////////////////////////////////////////
Expand Down Expand Up @@ -178,6 +179,11 @@ bool Test_IGFD_Utils_IsAValidCharForADigit_0() {
if (!IGFD::Utils::M_IsAValidCharForADigit('+')) return false;
if (!IGFD::Utils::M_IsAValidCharForADigit('.')) return false;
if (!IGFD::Utils::M_IsAValidCharForADigit('e')) return false;
if (!IGFD::Utils::M_IsAValidCharForADigit('E')) return false;
if (!IGFD::Utils::M_IsAValidCharForADigit('x')) return false;
if (!IGFD::Utils::M_IsAValidCharForADigit('X')) return false;
if (!IGFD::Utils::M_IsAValidCharForADigit('p')) return false;
if (!IGFD::Utils::M_IsAValidCharForADigit('P')) return false;
if (!IGFD::Utils::M_IsAValidCharForADigit('0')) return false;
if (!IGFD::Utils::M_IsAValidCharForADigit('1')) return false;
if (!IGFD::Utils::M_IsAValidCharForADigit('2')) return false;
Expand All @@ -200,7 +206,7 @@ bool Test_IGFD_Utils_IsAValidCharForADigit_0() {

// invalids numbers
bool Test_IGFD_Utils_ExtractNumFromStringAtPos_0() {
float n = 0.0f;
double n = 0.0;
size_t p = 0;
if (IGFD::Utils::M_ExtractNumFromStringAtPos("++2.5", p = 0, n)) return false;
if (IGFD::Utils::M_ExtractNumFromStringAtPos("--2.5", p = 0, n)) return false;
Expand All @@ -214,22 +220,48 @@ bool Test_IGFD_Utils_ExtractNumFromStringAtPos_0() {
if (IGFD::Utils::M_ExtractNumFromStringAtPos("-", p = 0, n)) return false;
if (IGFD::Utils::M_ExtractNumFromStringAtPos("e", p = 0, n)) return false;
if (IGFD::Utils::M_ExtractNumFromStringAtPos(".", p = 0, n)) return false;

// supported by strtod but will slow down more andwhy sorting by that
if (IGFD::Utils::M_ExtractNumFromStringAtPos("INF", p = 0, n)) return false;
if (IGFD::Utils::M_ExtractNumFromStringAtPos("NAN", p = 0, n)) return false;
return true;
}

// valids cases, verify number and new pos
// valid numbers
bool Test_IGFD_Utils_ExtractNumFromStringAtPos_1() {
float n = 0.0f;
double n = 0.0;
size_t p = 0;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos("+2.5", p = 0, n)) return false;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos("-2.5", p = 0, n)) return false;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos(".2.5", p = 0, n)) return false;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos("-2.5", p = 0, n)) return false;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos("+2.5", p = 0, n)) return false;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos("1e-5", p = 0, n)) return false;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos("1E32.5", p = 0, n)) return false;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos("0x14", p = 0, n)) return false;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos("0X14", p = 0, n)) return false;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos("2p2.5", p = 0, n)) return false;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos("2.5P-2.9", p = 0, n)) return false;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos("4588E4588", p = 0, n)) return false;
return true;
}

// valids cases, verify number and new pos
bool Test_IGFD_Utils_ExtractNumFromStringAtPos_2() {
double n = 0.0;
size_t p = 0;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos("abc-2.8abc", p = 3, n)) return false;
if (n != -2.8f) return false;
if (n != -2.8) return false;
if (p != 7U) return false;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos("abc+8.9abc", p = 3, n)) return false;
if (n != 8.9f) return false;
if (n != 8.9) return false;
if (p != 7U) return false;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos("abc10.6546abc", p = 3, n)) return false;
if (n != 10.6546f) return false;
if (n != 10.6546) return false;
if (p != 10U) return false;
if (!IGFD::Utils::M_ExtractNumFromStringAtPos("4588E4588", p = 3, n)) return false;
if (!std::isinf(n)) return false;
if (p != 9U) return false;
return true;
}

Expand Down Expand Up @@ -487,6 +519,7 @@ bool Test_Utils(const std::string& vTest) {
IfTestExist(Test_IGFD_Utils_IsAValidCharForADigit_0);
else IfTestExist(Test_IGFD_Utils_ExtractNumFromStringAtPos_0);
else IfTestExist(Test_IGFD_Utils_ExtractNumFromStringAtPos_1);
else IfTestExist(Test_IGFD_Utils_ExtractNumFromStringAtPos_2);
else IfTestExist(Test_IGFD_Utils_NaturalCompare_0);
else IfTestExist(Test_IGFD_Utils_NaturalCompare_1);
else IfTestExist(Test_IGFD_Utils_NaturalCompare_2);
Expand Down
4 changes: 2 additions & 2 deletions tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define GetCurrentDir _getcwd
#define SetCurrentDir _chdir
#define ROOT_SLASH "\\"
#elif defined(UNIX)
#else
#include<unistd.h> // chdir
#define GetCurrentDir getcwd
#define SetCurrentDir chdir
Expand All @@ -29,7 +29,7 @@ int main(int argc, char** argv) {
return Test_ImGuiFileDialog(argv[1]) ? 0 : 1;
}

return Test_ImGuiFileDialog("Test_IGFD_Utils_NaturalCompare_2") ? 0 : 1;
return Test_ImGuiFileDialog("Test_IGFD_Utils_ExtractNumFromStringAtPos_2") ? 0 : 1;

return 0;
}

0 comments on commit 64a3d01

Please sign in to comment.