-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make more JSON number handling improvements. (#136)
* Make more JSON number handling improvements. - Incorporate Ray's improvements to n_ftoa.c and n_atof.c that allow us to support larger exponents. - Add JAtoN and JNtoA unit tests to test these improvements. - If NOTE_C_LOW_MEM is defined, define JNUMBER to float. On truly low memory platforms, this likely does nothing, because those platforms often map double and float to the same underlying type (which is single-precision). However, when running the unit tests on a CPU with double-precision floating point support, this allows us to deliberately run the unit tests with JNUMBER set to single-precision. Doing this broke a couple test cases in JSON_number_handling_test.cpp, which I've fixed here. * Add NOTE_C_TEST_SINGLE_PRECISION. This `#define` forces `JNUMBER` to be `float`, rather than `double`. This allows us to run the unit tests with single precision floats, even on a machine that supports double precision. This is useful coverage because a lot of small MCUs don't support double precision. * Simplify JAtoN. Remove the code blocks guarded by `#ifndef LARGE_EXPONENT_FIX` (i.e. make the code behave like LARGE_EXPONENT_FIX is always defined). Remove the LARGE_EXPONENT_FIX define.
- Loading branch information
1 parent
5428d5a
commit c1c6d0a
Showing
10 changed files
with
189 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/*! | ||
* @file JAtoN_test.cpp | ||
* | ||
* Written by the Blues Inc. team. | ||
* | ||
* Copyright (c) 2024 Blues Inc. MIT License. Use of this source code is | ||
* governed by licenses granted by the copyright holder including that found in | ||
* the | ||
* <a href="https://github.com/blues/note-c/blob/master/LICENSE">LICENSE</a> | ||
* file. | ||
* | ||
*/ | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
#include <cmath> | ||
|
||
#include "n_lib.h" | ||
|
||
namespace | ||
{ | ||
|
||
#ifdef NOTE_C_LOW_MEM | ||
const JNUMBER expectedNums[] = { | ||
3.40282e+38, // Approximate largest single-precision float value | ||
1.17549e-38, // Approximate smallest single-precision float value | ||
}; | ||
|
||
const char *numStrs[] = { | ||
"3.40282e+38", | ||
"1.17549e-38" | ||
}; | ||
#else | ||
const JNUMBER expectedNums[] = { | ||
1.79769e+308, // Approximate largest double-precision float value | ||
2.22507e-308, // Approximate smallest double-precision float value | ||
}; | ||
|
||
const char *numStrs[] = { | ||
"1.79769e+308", | ||
"2.22507e-308" | ||
}; | ||
#endif // NOTE_C_LOW_MEM | ||
|
||
const size_t NUM_TESTS = sizeof(expectedNums) / sizeof(expectedNums[0]); | ||
const JNUMBER TOLERANCE = 1e-15; | ||
|
||
SCENARIO("JAtoN") | ||
{ | ||
for (size_t i = 0; i < NUM_TESTS; ++i) { | ||
GIVEN(std::string("The string to convert to a JNUMBER is ") + numStrs[i]) { | ||
WHEN("JAtoN is called on that string") { | ||
JNUMBER num = JAtoN(numStrs[i], NULL); | ||
|
||
THEN(std::string("The number returned is (approximately) ") + numStrs[i]) { | ||
JNUMBER diff = fabs(num - expectedNums[i]); | ||
JNUMBER err = diff / expectedNums[i]; | ||
CHECK(err < TOLERANCE); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/*! | ||
* @file JNtoA_test.cpp | ||
* | ||
* Written by the Blues Inc. team. | ||
* | ||
* Copyright (c) 2024 Blues Inc. MIT License. Use of this source code is | ||
* governed by licenses granted by the copyright holder including that found in | ||
* the | ||
* <a href="https://github.com/blues/note-c/blob/master/LICENSE">LICENSE</a> | ||
* file. | ||
* | ||
*/ | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
#include <cmath> | ||
|
||
#include "n_lib.h" | ||
|
||
namespace | ||
{ | ||
|
||
#ifdef NOTE_C_LOW_MEM | ||
const JNUMBER nums[] = { | ||
3.40282e+38, // Approximate largest single-precision float value | ||
1.17549e-38, // Approximate smallest single-precision float value | ||
}; | ||
|
||
const char *expectedNumStrs[] = { | ||
"3.40282e+38", | ||
"1.17549e-38" | ||
}; | ||
|
||
const JNUMBER TOLERANCE = 1e-8; | ||
#else | ||
const JNUMBER nums[] = { | ||
1.79769e+308, // Approximate largest double-precision float value | ||
2.22507e-308, // Approximate smallest double-precision float value | ||
}; | ||
|
||
const char *expectedNumStrs[] = { | ||
"1.79769e+308", | ||
"2.22507e-308" | ||
}; | ||
|
||
const JNUMBER TOLERANCE = 1e-8; | ||
#endif // NOTE_C_LOW_MEM | ||
|
||
const size_t NUM_TESTS = sizeof(nums) / sizeof(nums[0]); | ||
|
||
SCENARIO("JNtoA") | ||
{ | ||
char numStr[JNTOA_MAX] = {0}; | ||
|
||
for (size_t i = 0; i < NUM_TESTS; ++i) { | ||
GIVEN(std::string("The number to convert to a string is ") + expectedNumStrs[i]) { | ||
WHEN("JNtoA is called on that number") { | ||
JNtoA(nums[i], numStr, -1); | ||
|
||
THEN(std::string("The string returned is (approximately)") + expectedNumStrs[i]) { | ||
double extractedNum; | ||
sscanf(expectedNumStrs[i], "%lf", &extractedNum); | ||
JNUMBER diff = fabs(extractedNum - nums[i]); | ||
JNUMBER err = diff / nums[i]; | ||
CHECK(err < TOLERANCE); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters