tests/test_string.c

changeset 1052
e997862a57d8
parent 1003
455afb252878
equal deleted inserted replaced
1051:7d17bd1103d7 1052:e997862a57d8
28 28
29 #include "cx/test.h" 29 #include "cx/test.h"
30 #include "util_allocator.h" 30 #include "util_allocator.h"
31 31
32 #include "cx/string.h" 32 #include "cx/string.h"
33
34 #include <limits.h>
35 #include <errno.h>
33 36
34 #define ASSERT_ZERO_TERMINATED(str) CX_TEST_ASSERTM((str).ptr[(str).length] == '\0', \ 37 #define ASSERT_ZERO_TERMINATED(str) CX_TEST_ASSERTM((str).ptr[(str).length] == '\0', \
35 #str " is not zero terminated") 38 #str " is not zero terminated")
36 39
37 CX_TEST(test_string_construct) { 40 CX_TEST(test_string_construct) {
968 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(str), CX_STR("AN,ARBITRARILY;||SEPARATED;STRING"))); 971 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(str), CX_STR("AN,ARBITRARILY;||SEPARATED;STRING")));
969 } 972 }
970 cx_strfree(&str); 973 cx_strfree(&str);
971 } 974 }
972 975
976 #define test_strtoint_impl(suffix, num, base, var, min, max) \
977 do { \
978 errno = 0; \
979 int r = cx_strto##var(cx_str( #num), &var, base); \
980 if ((min) <= (num##suffix) && (num##suffix) <= (max)) { \
981 CX_TEST_ASSERTM(0 == r, "failed for "#num); \
982 CX_TEST_ASSERT(0 == errno); \
983 CX_TEST_ASSERT((num##suffix) == (0##suffix)+(var)); \
984 } else { \
985 CX_TEST_ASSERTM(0 != r, "out-of-range not detected for "#num " in variant "#var); \
986 CX_TEST_ASSERT(ERANGE == errno); \
987 } \
988 } while (0)
989
990 #define test_strtoint_rollout_signed_impl(num, base) \
991 test_strtoint_impl(LL, num, base, s, SHRT_MIN, SHRT_MAX); \
992 test_strtoint_impl(LL, num, base, i, INT_MIN, INT_MAX); \
993 test_strtoint_impl(LL, num, base, l, LONG_MIN, LONG_MAX); \
994 test_strtoint_impl(LL, num, base, ll, LLONG_MIN, LLONG_MAX); \
995 test_strtoint_impl(LL, num, base, i8, INT8_MIN, INT8_MAX); \
996 test_strtoint_impl(LL, num, base, i16, INT16_MIN, INT16_MAX); \
997 test_strtoint_impl(LL, num, base, i32, INT32_MIN, INT32_MAX); \
998 test_strtoint_impl(LL, num, base, i64, INT64_MIN, INT64_MAX); \
999 test_strtoint_impl(LL, num, base, z, -SSIZE_MAX-1, SSIZE_MAX)
1000
1001 #define test_strtoint_rollout_signed(num, base) \
1002 test_strtoint_rollout_signed_impl(num, base); \
1003 test_strtoint_rollout_signed_impl(-num, base)
1004
1005 #define test_strtoint_rollout(num, base) \
1006 test_strtoint_impl(ULL, num, base, us, 0, USHRT_MAX); \
1007 test_strtoint_impl(ULL, num, base, u, 0, UINT_MAX); \
1008 test_strtoint_impl(ULL, num, base, ul, 0, ULONG_MAX); \
1009 test_strtoint_impl(ULL, num, base, ull, 0, ULLONG_MAX); \
1010 test_strtoint_impl(ULL, num, base, u8, 0, UINT8_MAX); \
1011 test_strtoint_impl(ULL, num, base, u16, 0, UINT16_MAX); \
1012 test_strtoint_impl(ULL, num, base, u32, 0, UINT32_MAX); \
1013 test_strtoint_impl(ULL, num, base, u64, 0, UINT64_MAX); \
1014 test_strtoint_impl(ULL, num, base, uz, 0, SIZE_MAX)
1015
1016 CX_TEST(test_string_to_signed_integer) {
1017 short s;
1018 int i;
1019 long l;
1020 long long ll;
1021 int8_t i8;
1022 int16_t i16;
1023 int32_t i32;
1024 int64_t i64;
1025 ssize_t z;
1026 CX_TEST_DO {
1027 // do some brute force tests with all ranges
1028 test_strtoint_rollout_signed(47, 10);
1029 test_strtoint_rollout_signed(210, 10);
1030 test_strtoint_rollout_signed(5678, 10);
1031 test_strtoint_rollout_signed(40678, 10);
1032 test_strtoint_rollout_signed(1350266537, 10);
1033 test_strtoint_rollout_signed(3350266537, 10);
1034 test_strtoint_rollout_signed(473350266537, 10);
1035 test_strtoint_rollout_signed(057, 8);
1036 test_strtoint_rollout_signed(0322, 8);
1037 test_strtoint_rollout_signed(013056, 8);
1038 test_strtoint_rollout_signed(0117346, 8);
1039 test_strtoint_rollout_signed(012036667251, 8);
1040 test_strtoint_rollout_signed(030754201251, 8);
1041 test_strtoint_rollout_signed(06706567757251, 8);
1042 test_strtoint_rollout_signed(0767716340165362204025, 8);
1043 test_strtoint_rollout_signed(0x65, 16);
1044 test_strtoint_rollout_signed(0xf5, 16);
1045 test_strtoint_rollout_signed(0xABC5, 16);
1046 test_strtoint_rollout_signed(0xFBC5, 16);
1047 test_strtoint_rollout_signed(0x6df9CE03, 16);
1048 test_strtoint_rollout_signed(0xFdf9CE03, 16);
1049 test_strtoint_rollout_signed(0x6df9CE03AbC90815, 16);
1050 // TODO: roll out base 2 tests, but that needs C23
1051
1052 // do some special case tests
1053
1054 // can fit only in unsigned long long
1055 errno = 0;
1056 CX_TEST_ASSERT(0 != cx_strtoll(cx_str("0x8df9CE03AbC90815"), &ll, 16));
1057 CX_TEST_ASSERT(errno == ERANGE);
1058
1059 // TODO: implement more special cases
1060 }
1061 }
1062
1063 CX_TEST(test_string_to_unsigned_integer) {
1064 unsigned short us;
1065 unsigned int u;
1066 unsigned long ul;
1067 unsigned long long ull;
1068 uint8_t u8;
1069 uint16_t u16;
1070 uint32_t u32;
1071 uint64_t u64;
1072 size_t uz;
1073 CX_TEST_DO {
1074 // do some brute force tests with all ranges
1075 test_strtoint_rollout(47, 10);
1076 test_strtoint_rollout(210, 10);
1077 test_strtoint_rollout(5678, 10);
1078 test_strtoint_rollout(40678, 10);
1079 test_strtoint_rollout(1350266537, 10);
1080 test_strtoint_rollout(3350266537, 10);
1081 test_strtoint_rollout(473350266537, 10);
1082 test_strtoint_rollout(057, 8);
1083 test_strtoint_rollout(0322, 8);
1084 test_strtoint_rollout(013056, 8);
1085 test_strtoint_rollout(0117346, 8);
1086 test_strtoint_rollout(012036667251, 8);
1087 test_strtoint_rollout(030754201251, 8);
1088 test_strtoint_rollout(06706567757251, 8);
1089 test_strtoint_rollout(01767716340165362204025, 8);
1090 test_strtoint_rollout(0x65, 16);
1091 test_strtoint_rollout(0xf5, 16);
1092 test_strtoint_rollout(0xABC5, 16);
1093 test_strtoint_rollout(0xFBC5, 16);
1094 test_strtoint_rollout(0x6df9CE03, 16);
1095 test_strtoint_rollout(0xFdf9CE03, 16);
1096 test_strtoint_rollout(0x6df9CE03AbC90815, 16);
1097 test_strtoint_rollout(0xfdf9CE03AbC90815, 16);
1098 // TODO: roll out base 2 tests, but that needs C23
1099
1100 // do some special case tests
1101
1102 // TODO: implement tests
1103 }
1104 }
1105
1106 CX_TEST(test_string_to_float) {
1107 float f;
1108 CX_TEST_DO {
1109 CX_TEST_ASSERT(0 == cx_strtof(cx_str("11.3"), &f));
1110 CX_TEST_ASSERT(11.3f == f);
1111 }
1112 }
1113
1114 CX_TEST(test_string_to_double) {
1115 double d;
1116 CX_TEST_DO {
1117 CX_TEST_ASSERT(0 == cx_strtod(cx_str("11.3"), &d));
1118 CX_TEST_ASSERT(11.3 == d);
1119 }
1120 }
1121
1122 CX_TEST(test_string_to_float_german) {
1123 float f;
1124 CX_TEST_DO {
1125 // TODO: implement
1126 (void)f;
1127 }
1128 }
973 1129
974 CxTestSuite *cx_test_suite_string(void) { 1130 CxTestSuite *cx_test_suite_string(void) {
975 CxTestSuite *suite = cx_test_suite_new("string"); 1131 CxTestSuite *suite = cx_test_suite_new("string");
976 1132
977 cx_test_register(suite, test_string_construct); 1133 cx_test_register(suite, test_string_construct);
1004 cx_test_register(suite, test_strtok_next_unlimited); 1160 cx_test_register(suite, test_strtok_next_unlimited);
1005 cx_test_register(suite, test_strtok_next_advanced); 1161 cx_test_register(suite, test_strtok_next_advanced);
1006 1162
1007 return suite; 1163 return suite;
1008 } 1164 }
1165
1166 CxTestSuite *cx_test_suite_string_to_number(void) {
1167 CxTestSuite *suite = cx_test_suite_new("string to number");
1168
1169 cx_test_register(suite, test_string_to_signed_integer);
1170 cx_test_register(suite, test_string_to_unsigned_integer);
1171 cx_test_register(suite, test_string_to_float);
1172 cx_test_register(suite, test_string_to_double);
1173 cx_test_register(suite, test_string_to_float_german);
1174
1175 return suite;
1176 }

mercurial