Conversion functions

This commit is contained in:
2026-09-12 10:08:59 -05:00
parent fd010ec112
commit 838861aeea
3 changed files with 377 additions and 2 deletions
+195 -1
View File
@@ -366,6 +366,201 @@ MICROTEST(strview_contains, false_when_other_longer_than_self) {
MICROTEST_EXPECT_FALSE(strview_contains(&self, &other));
}
/* -------------------------------------------------------------------------
* strview_to_int64()
* ---------------------------------------------------------------------- */
MICROTEST(strview_to_int64, positive_integer_succeeds) {
strview_t view = strview_from_cstr("12345");
malunal_int64_t out = 0;
error_t err = strview_to_int64(&view, &out);
MICROTEST_EXPECT_NULL(err.domain);
MICROTEST_EXPECT_EQ(out, (malunal_int64_t)12345);
}
MICROTEST(strview_to_int64, negative_integer_succeeds) {
strview_t view = strview_from_cstr("-42");
malunal_int64_t out = 0;
error_t err = strview_to_int64(&view, &out);
MICROTEST_EXPECT_NULL(err.domain);
MICROTEST_EXPECT_EQ(out, (malunal_int64_t)-42);
}
MICROTEST(strview_to_int64, zero_succeeds) {
strview_t view = strview_from_cstr("0");
malunal_int64_t out = -1;
error_t err = strview_to_int64(&view, &out);
MICROTEST_EXPECT_NULL(err.domain);
MICROTEST_EXPECT_EQ(out, (malunal_int64_t)0);
}
MICROTEST(strview_to_int64, succeeds_on_subview_not_ending_at_buffer_end) {
malunal_cstr_t data = "12345 and then some trailing text";
strview_t view = strview(data, 5);
malunal_int64_t out = 0;
error_t err = strview_to_int64(&view, &out);
MICROTEST_EXPECT_NULL(err.domain);
MICROTEST_EXPECT_EQ(out, (malunal_int64_t)12345);
}
MICROTEST(strview_to_int64, trailing_garbage_reports_unconsumed) {
strview_t view = strview_from_cstr("123abc");
malunal_int64_t out = 0;
error_t err = strview_to_int64(&view, &out);
MICROTEST_EXPECT_NOT_NULL(err.domain);
MICROTEST_EXPECT_EQ(err.code, (malunal_int32_t)STRVIEW_ERROR_UNCONSUMED);
}
MICROTEST(strview_to_int64, non_numeric_reports_unconsumed) {
strview_t view = strview_from_cstr("abc");
malunal_int64_t out = 0;
error_t err = strview_to_int64(&view, &out);
MICROTEST_EXPECT_NOT_NULL(err.domain);
MICROTEST_EXPECT_EQ(err.code, (malunal_int32_t)STRVIEW_ERROR_UNCONSUMED);
}
MICROTEST(strview_to_int64, empty_view_reports_unconsumed) {
strview_t view = strview_from_cstr("");
malunal_int64_t out = 0;
error_t err = strview_to_int64(&view, &out);
MICROTEST_EXPECT_NOT_NULL(err.domain);
MICROTEST_EXPECT_EQ(err.code, (malunal_int32_t)STRVIEW_ERROR_UNCONSUMED);
}
MICROTEST(strview_to_int64, null_self_reports_null_input) {
malunal_int64_t out = 99;
error_t err = strview_to_int64(null, &out);
MICROTEST_EXPECT_NOT_NULL(err.domain);
MICROTEST_EXPECT_EQ(err.code, (malunal_int32_t)STRVIEW_ERROR_NULL_INPUT);
MICROTEST_EXPECT_EQ(out, (malunal_int64_t)0);
}
MICROTEST(strview_to_int64, overflow_reports_out_of_range) {
strview_t view = strview_from_cstr("99999999999999999999999");
malunal_int64_t out = 0;
error_t err = strview_to_int64(&view, &out);
MICROTEST_EXPECT_NOT_NULL(err.domain);
MICROTEST_EXPECT_EQ(err.code, (malunal_int32_t)STRVIEW_ERROR_OUT_OF_RANGE);
}
MICROTEST(strview_to_int64, underflow_reports_out_of_range) {
strview_t view = strview_from_cstr("-99999999999999999999999");
malunal_int64_t out = 0;
error_t err = strview_to_int64(&view, &out);
MICROTEST_EXPECT_NOT_NULL(err.domain);
MICROTEST_EXPECT_EQ(err.code, (malunal_int32_t)STRVIEW_ERROR_OUT_OF_RANGE);
}
MICROTEST(strview_to_int64, null_out_pointer_does_not_crash) {
strview_t view = strview_from_cstr("12345");
error_t err = strview_to_int64(&view, null);
MICROTEST_EXPECT_NULL(err.domain);
}
/* -------------------------------------------------------------------------
* strview_to_double()
* ---------------------------------------------------------------------- */
MICROTEST(strview_to_double, positive_decimal_succeeds) {
strview_t view = strview_from_cstr("3.14");
malunal_double_t out = 0.0;
error_t err = strview_to_double(&view, &out);
MICROTEST_EXPECT_NULL(err.domain);
MICROTEST_EXPECT_TRUE(out > 3.139 && out < 3.141);
}
MICROTEST(strview_to_double, negative_decimal_succeeds) {
strview_t view = strview_from_cstr("-2.5");
malunal_double_t out = 0.0;
error_t err = strview_to_double(&view, &out);
MICROTEST_EXPECT_NULL(err.domain);
MICROTEST_EXPECT_TRUE(out > -2.501 && out < -2.499);
}
MICROTEST(strview_to_double, integer_like_value_succeeds) {
strview_t view = strview_from_cstr("42");
malunal_double_t out = 0.0;
error_t err = strview_to_double(&view, &out);
MICROTEST_EXPECT_NULL(err.domain);
MICROTEST_EXPECT_TRUE(out > 41.999 && out < 42.001);
}
MICROTEST(strview_to_double, succeeds_on_subview_not_ending_at_buffer_end) {
malunal_cstr_t data = "3.14 and then some trailing text";
strview_t view = strview(data, 4);
malunal_double_t out = 0.0;
error_t err = strview_to_double(&view, &out);
MICROTEST_EXPECT_NULL(err.domain);
MICROTEST_EXPECT_TRUE(out > 3.139 && out < 3.141);
}
MICROTEST(strview_to_double, trailing_garbage_reports_unconsumed) {
strview_t view = strview_from_cstr("3.14xyz");
malunal_double_t out = 0.0;
error_t err = strview_to_double(&view, &out);
MICROTEST_EXPECT_NOT_NULL(err.domain);
MICROTEST_EXPECT_EQ(err.code, (malunal_int32_t)STRVIEW_ERROR_UNCONSUMED);
}
MICROTEST(strview_to_double, non_numeric_reports_unconsumed) {
strview_t view = strview_from_cstr("abc");
malunal_double_t out = 0.0;
error_t err = strview_to_double(&view, &out);
MICROTEST_EXPECT_NOT_NULL(err.domain);
MICROTEST_EXPECT_EQ(err.code, (malunal_int32_t)STRVIEW_ERROR_UNCONSUMED);
}
MICROTEST(strview_to_double, empty_view_reports_unconsumed) {
strview_t view = strview_from_cstr("");
malunal_double_t out = 0.0;
error_t err = strview_to_double(&view, &out);
MICROTEST_EXPECT_NOT_NULL(err.domain);
MICROTEST_EXPECT_EQ(err.code, (malunal_int32_t)STRVIEW_ERROR_UNCONSUMED);
}
MICROTEST(strview_to_double, null_self_reports_null_input) {
malunal_double_t out = 99.0;
error_t err = strview_to_double(null, &out);
MICROTEST_EXPECT_NOT_NULL(err.domain);
MICROTEST_EXPECT_EQ(err.code, (malunal_int32_t)STRVIEW_ERROR_NULL_INPUT);
MICROTEST_EXPECT_TRUE(out == 0.0);
}
MICROTEST(strview_to_double, overflow_reports_out_of_range) {
strview_t view = strview_from_cstr("1e400");
malunal_double_t out = 0.0;
error_t err = strview_to_double(&view, &out);
MICROTEST_EXPECT_NOT_NULL(err.domain);
MICROTEST_EXPECT_EQ(err.code, (malunal_int32_t)STRVIEW_ERROR_OUT_OF_RANGE);
}
MICROTEST(strview_to_double, null_out_pointer_does_not_crash) {
strview_t view = strview_from_cstr("3.14");
error_t err = strview_to_double(&view, null);
MICROTEST_EXPECT_NULL(err.domain);
}
/* -------------------------------------------------------------------------
* STRVIEW_NPOS
* ---------------------------------------------------------------------- */
@@ -375,4 +570,3 @@ MICROTEST(strview_npos, is_max_size_value) {
}
MICROTEST_MAIN()