tests/test_string.c

changeset 1061
c7d23892eab5
parent 1052
e997862a57d8
child 1063
e453e717876e
equal deleted inserted replaced
1060:0a7c1bb2372d 1061:c7d23892eab5
1048 test_strtoint_rollout_signed(0xFdf9CE03, 16); 1048 test_strtoint_rollout_signed(0xFdf9CE03, 16);
1049 test_strtoint_rollout_signed(0x6df9CE03AbC90815, 16); 1049 test_strtoint_rollout_signed(0x6df9CE03AbC90815, 16);
1050 // TODO: roll out base 2 tests, but that needs C23 1050 // TODO: roll out base 2 tests, but that needs C23
1051 1051
1052 // do some special case tests 1052 // do some special case tests
1053 // --------------------------
1053 1054
1054 // can fit only in unsigned long long 1055 // can fit only in unsigned long long
1055 errno = 0; 1056 errno = 0;
1056 CX_TEST_ASSERT(0 != cx_strtoll(cx_str("0x8df9CE03AbC90815"), &ll, 16)); 1057 CX_TEST_ASSERT(0 != cx_strtoll(cx_str("0x8df9CE03AbC90815"), &ll, 16));
1057 CX_TEST_ASSERT(errno == ERANGE); 1058 CX_TEST_ASSERT(errno == ERANGE);
1058 1059
1059 // TODO: implement more special cases 1060 // edge case: only the sign bit is set
1061 errno = 0;
1062 CX_TEST_ASSERT(0 != cx_strtoi16(cx_str("0x8000"), &i16, 16));
1063 CX_TEST_ASSERT(errno == ERANGE);
1064 errno = 0;
1065 CX_TEST_ASSERT(0 == cx_strtoi16(cx_str("-0x8000"), &i16, 16));
1066 CX_TEST_ASSERT(errno == 0);
1067 CX_TEST_ASSERT(i16 == INT16_MIN);
1068 errno = 0;
1069 CX_TEST_ASSERT(0 != cx_strtoi64(cx_str("X8000000000000000"), &i64, 16));
1070 CX_TEST_ASSERT(errno == ERANGE);
1071 errno = 0;
1072 CX_TEST_ASSERT(0 == cx_strtoi64(cx_str("-X8000000000000000"), &i64, 16));
1073 CX_TEST_ASSERT(errno == 0);
1074 CX_TEST_ASSERT(i64 == INT64_MIN);
1075
1076 // group separators
1077 CX_TEST_ASSERT(0 == cx_strtoi32(cx_str(" -123,456"), &i32, 10));
1078 CX_TEST_ASSERT(i32 == -123456);
1079 errno = 0;
1080 CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str(" -Xab,cd"), &i16, 16, "'"));
1081 CX_TEST_ASSERT(errno == EINVAL);
1082 errno = 0;
1083 CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str(" -X'ab'cd"), &i16, 16, "'"));
1084 CX_TEST_ASSERT(errno == ERANGE);
1085 errno = 0;
1086 CX_TEST_ASSERT(0 == cx_strtoi16_lc(cx_str(" -X'67'89"), &i16, 16, "'"));
1087 CX_TEST_ASSERT(errno == 0);
1088 CX_TEST_ASSERT(i16 == -0x6789);
1089
1090 // binary and (unusual notation of) signed binary
1091 errno = 0;
1092 CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str(" -1010 1011"), &i8, 2, " "));
1093 CX_TEST_ASSERT(errno == ERANGE);
1094 errno = 0;
1095 CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str(" 1010 1011"), &i8, 2, " "));
1096 CX_TEST_ASSERT(errno == ERANGE);
1097 errno = 0;
1098 CX_TEST_ASSERT(0 == cx_strtoi8_lc(cx_str(" -0101 0101"), &i8, 2, " "));
1099 CX_TEST_ASSERT(errno == 0);
1100 CX_TEST_ASSERT(i8 == -0x55);
1060 } 1101 }
1061 } 1102 }
1062 1103
1063 CX_TEST(test_string_to_unsigned_integer) { 1104 CX_TEST(test_string_to_unsigned_integer) {
1064 unsigned short us; 1105 unsigned short us;
1096 test_strtoint_rollout(0x6df9CE03AbC90815, 16); 1137 test_strtoint_rollout(0x6df9CE03AbC90815, 16);
1097 test_strtoint_rollout(0xfdf9CE03AbC90815, 16); 1138 test_strtoint_rollout(0xfdf9CE03AbC90815, 16);
1098 // TODO: roll out base 2 tests, but that needs C23 1139 // TODO: roll out base 2 tests, but that needs C23
1099 1140
1100 // do some special case tests 1141 // do some special case tests
1101 1142 // --------------------------
1102 // TODO: implement tests 1143
1144 // group separators
1145 CX_TEST_ASSERT(0 == cx_strtou32(cx_str(" 123,456"), &u32, 10));
1146 CX_TEST_ASSERT(u32 == 123456);
1147 errno = 0;
1148 CX_TEST_ASSERT(0 != cx_strtou16_lc(cx_str(" ab,cd"), &u16, 16, "'"));
1149 CX_TEST_ASSERT(errno == EINVAL);
1150 errno = 0;
1151 CX_TEST_ASSERT(0 == cx_strtou16_lc(cx_str(" ab'cd"), &u16, 16, "'"));
1152 CX_TEST_ASSERT(errno == 0);
1153 CX_TEST_ASSERT(u16 == 0xabcd);
1154
1155 // binary
1156 errno = 0;
1157 CX_TEST_ASSERT(0 != cx_strtou8_lc(cx_str("1 1010 1011"), &u8, 2, " "));
1158 CX_TEST_ASSERT(errno == ERANGE);
1159 errno = 0;
1160 CX_TEST_ASSERT(0 == cx_strtou8_lc(cx_str(" 1010 1011"), &u8, 2, " "));
1161 CX_TEST_ASSERT(errno == 0);
1162 CX_TEST_ASSERT(u8 == 0xAB);
1103 } 1163 }
1104 } 1164 }
1105 1165
1106 CX_TEST(test_string_to_float) { 1166 CX_TEST(test_string_to_float) {
1107 float f; 1167 float f;
1108 CX_TEST_DO { 1168 CX_TEST_DO {
1109 CX_TEST_ASSERT(0 == cx_strtof(cx_str("11.3"), &f)); 1169 CX_TEST_ASSERT(0 == cx_strtof(cx_str("11.3"), &f));
1110 CX_TEST_ASSERT(11.3f == f); 1170 CX_TEST_ASSERT(11.3f == f);
1171
1172 CX_TEST_ASSERT(0 == cx_strtof(cx_str("1.67262192595e-27"), &f));
1173 CX_TEST_ASSERT(1.67262192595e-27f == f);
1174
1175 CX_TEST_ASSERT(0 == cx_strtof_lc(cx_str("138,339.4"), &f, '.', ","));
1176 CX_TEST_ASSERT(138339.4f == f);
1177
1178 CX_TEST_ASSERT(0 == cx_strtof_lc(cx_str("138,339.4"), &f, ',', "."));
1179 CX_TEST_ASSERT(138.3394f == f);
1111 } 1180 }
1112 } 1181 }
1113 1182
1114 CX_TEST(test_string_to_double) { 1183 CX_TEST(test_string_to_double) {
1115 double d; 1184 double d;
1116 CX_TEST_DO { 1185 CX_TEST_DO {
1117 CX_TEST_ASSERT(0 == cx_strtod(cx_str("11.3"), &d)); 1186 CX_TEST_ASSERT(0 == cx_strtod(cx_str("11.3"), &d));
1118 CX_TEST_ASSERT(11.3 == d); 1187 CX_TEST_ASSERT(11.3 == d);
1188
1189 CX_TEST_ASSERT(0 == cx_strtod(cx_str("1.67262192595e-27"), &d));
1190 CX_TEST_ASSERT(1.67262192595e-27 == d);
1191
1192 CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, '.', ","));
1193 CX_TEST_ASSERT(138339.4 == d);
1194
1195 CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, ',', "."));
1196 CX_TEST_ASSERT(138.3394 == d);
1119 } 1197 }
1120 } 1198 }
1121 1199
1122 CX_TEST(test_string_to_float_german) { 1200 CX_TEST(test_string_to_float_german) {
1123 float f; 1201 float f;

mercurial