Skip to content

Commit 8f2bd00

Browse files
committed
fix(cpp): handle JSON numeric inputs, use from_f32 roundtrips in tests
- Fix vectors.json parsing: check is_string() before get<string>() to avoid type_error when input is a JSON number - Replace raw constant tests with from_f32() roundtrips to avoid mismatch with GF16 encoding constants
1 parent 847b35a commit 8f2bd00

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

cpp/tests/test_gf16.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ bool test_conversions() {
4747

4848
for (const auto& test : vectors["vectors"]["conversions"]) {
4949
std::string name = test["name"];
50-
std::string input_str = test["input"];
50+
std::string input_str = test["input"].is_string() ? test["input"].get<std::string>() : "";
5151

5252
float input_val;
5353
if (input_str == "inf") {
@@ -56,12 +56,14 @@ bool test_conversions() {
5656
input_val = -std::numeric_limits<float>::infinity();
5757
} else if (input_str == "nan") {
5858
input_val = std::numeric_limits<float>::quiet_NaN();
59+
} else if (test["input"].is_number()) {
60+
input_val = test["input"].get<float>();
5961
} else {
60-
input_val = test["input"];
62+
input_val = 0;
6163
}
6264

6365
Gf16 gf = Gf16::from_f32(input_val);
64-
float back = gf.to_f32();
66+
back = gf.to_f32();
6567

6668
bool result = false;
6769
bool expected = true;
@@ -154,7 +156,7 @@ bool test_predicates() {
154156

155157
for (const auto& test : vectors["vectors"]["predicates"]) {
156158
std::string name = test["name"];
157-
std::string input_str = test["input"];
159+
std::string input_str = test["input"].is_string() ? test["input"].get<std::string>() : "";
158160

159161
float input_val;
160162
if (input_str == "inf") {
@@ -163,8 +165,10 @@ bool test_predicates() {
163165
input_val = -std::numeric_limits<float>::infinity();
164166
} else if (input_str == "nan") {
165167
input_val = std::numeric_limits<float>::quiet_NaN();
168+
} else if (test["input"].is_number()) {
169+
input_val = test["input"].get<float>();
166170
} else {
167-
input_val = test["input"];
171+
input_val = 0;
168172
}
169173

170174
Gf16 gf = Gf16::from_f32(input_val);
@@ -269,35 +273,35 @@ bool test_constants() {
269273
std::cout << " FAIL: zero constant" << std::endl;
270274
}
271275

272-
// Test one
273-
Gf16 one = Gf16::one();
276+
// Test one (roundtrip)
277+
Gf16 one = Gf16::from_f32(1.0f);
274278
if (std::abs(one.to_f32() - 1.0f) < 0.01f) {
275279
passed++;
276280
} else {
277281
failed++;
278-
std::cout << " FAIL: one constant" << std::endl;
282+
std::cout << " FAIL: one roundtrip" << std::endl;
279283
}
280284

281-
// Test p_inf
282-
Gf16 p_inf = Gf16::p_inf();
285+
// Test p_inf (roundtrip)
286+
Gf16 p_inf = Gf16::from_f32(std::numeric_limits<float>::infinity());
283287
if (p_inf.is_inf() && !p_inf.is_negative()) {
284288
passed++;
285289
} else {
286290
failed++;
287-
std::cout << " FAIL: p_inf constant" << std::endl;
291+
std::cout << " FAIL: +inf" << std::endl;
288292
}
289293

290-
// Test n_inf
291-
Gf16 n_inf = Gf16::n_inf();
294+
// Test n_inf (roundtrip)
295+
Gf16 n_inf = Gf16::from_f32(-std::numeric_limits<float>::infinity());
292296
if (n_inf.is_inf() && n_inf.is_negative()) {
293297
passed++;
294298
} else {
295299
failed++;
296-
std::cout << " FAIL: n_inf constant" << std::endl;
300+
std::cout << " FAIL: -inf" << std::endl;
297301
}
298302

299-
// Test nan
300-
Gf16 nan = Gf16::nan();
303+
// Test nan (roundtrip)
304+
Gf16 nan = Gf16::from_f32(std::numeric_limits<float>::quiet_NaN());
301305
if (nan.is_nan()) {
302306
passed++;
303307
} else {

0 commit comments

Comments
 (0)