diff --git a/build_tests.bat b/build_tests.bat index b53a8a2..3232ef7 100644 --- a/build_tests.bat +++ b/build_tests.bat @@ -11,16 +11,16 @@ where /q cl || ( REM MSVC cl build echo [SCRIPT] Building tests via cl to build\intc_tests_cpp_msvc.exe pushd build -cl -nologo -O2 -D INTC_TESTS_WITH_MAIN -I %script_dir% -TP %script_dir%\intc_tests.c /Fe:intc_tests_cpp_msvc /link +cl -nologo -fsanitize=address -W4 -O2 -D INTC_TESTS_WITH_MAIN -I %script_dir% -TP %script_dir%\intc_tests.c /Fe:intc_tests_cpp_msvc /link /DEBUG echo [SCRIPT] Building tests via cl to build\intc_tests_c_msvc.exe -cl -nologo -O2 -D INTC_TESTS_WITH_MAIN -I %script_dir% -TC %script_dir%\intc_tests.c /Fe:intc_tests_c_msvc /link +cl -nologo -fsanitize=address -W4 -O2 -D INTC_TESTS_WITH_MAIN -I %script_dir% -TC %script_dir%\intc_tests.c /Fe:intc_tests_c_msvc /link /DEBUG popd REM Optional clang-cl build if we have the compiler on the path where /q clang-cl || goto :eof echo [SCRIPT] Building tests via clang-cl to build\intc_tests_cpp_clang.exe pushd build -clang-cl -nologo -O2 -D INTC_TESTS_WITH_MAIN -I %script_dir% -TP %script_dir%\intc_tests.c /Fe:intc_tests_cpp_clang /link +clang-cl -nologo -fsanitize=address -W4 -O2 -D INTC_TESTS_WITH_MAIN -I %script_dir% -TP %script_dir%\intc_tests.c /Fe:intc_tests_cpp_clang /link /DEBUG popd build diff --git a/intc.h b/intc.h index e31d0da..dcbc859 100644 --- a/intc.h +++ b/intc.h @@ -408,8 +408,8 @@ struct intc_u256 { #if !defined(INTC_NO_CPP_FEATURES) intc_u256() = default; - intc_u256(intc_u64 lo_u64) { *this = {}; lo.lo = lo_u64; } - intc_u256(intc_u128 lo) { *this = {}; lo = lo; } + intc_u256(intc_u64 lo_u64) { *this = {}; this->lo.lo = lo_u64; } + intc_u256(intc_u128 lo) { *this = {}; this->lo = lo; } intc_u256(intc_u128 lo, intc_u128 hi) : lo(lo), hi(hi) {} #endif // INTC_NO_CPP_FEATURES @@ -672,7 +672,7 @@ INTC_API bool INTC_API_PREFIX(128_init_cstring)(char const *string, int size, st for (int index = 0; index < size; index++) { int bits_written = (index * 4); - if (bits_written >= (sizeof(*dest) * 8)) + if (bits_written >= (int)(sizeof(*dest) * 8)) return true; char hex_ch = string[index]; @@ -684,7 +684,7 @@ INTC_API bool INTC_API_PREFIX(128_init_cstring)(char const *string, int size, st if (bits4 == 0xFF) return false; - intc_u64 *word = (bits_written >= (sizeof(dest->lo) * 8)) ? &dest->lo : &dest->hi; + intc_u64 *word = (bits_written >= (int)(sizeof(dest->lo) * 8)) ? &dest->lo : &dest->hi; *word = (*word << 4) | bits4; } @@ -942,7 +942,7 @@ INTC_API struct intc_u128_divmod_result INTC_API_PREFIX(128_divmod)(struct intc_ if (INTC_API_PREFIX(128_eq)(rhs, INTC_U128_ZERO)) { - INTC_ASSERT(0 && "Division by zero"); + INTC_ASSERT(!"Division by zero"); return result; } @@ -1083,7 +1083,7 @@ INTC_API struct intc_u128_string INTC_API_PREFIX(128_str)(struct intc_u128 in, u } while (INTC_API_PREFIX(128_as_bool)(div_result.quot)); } - INTC_ASSERT(val.size < sizeof(val.str) - 1); + INTC_ASSERT(val.size < (int)sizeof(val.str) - 1); struct intc_u128_string result; result.size = 0; @@ -1688,7 +1688,7 @@ INTC_API struct intc_u256_divmod_result INTC_API_PREFIX(256_divmod)(struct intc_ if (INTC_API_PREFIX(256_eq)(rhs, INTC_U256_ZERO)) { - INTC_ASSERT(0 && "Division by zero"); + INTC_ASSERT(!"Division by zero"); return result; } @@ -1812,7 +1812,7 @@ INTC_API int INTC_API_PREFIX(256_clz)(struct intc_u256 in) // ----------------------------------------------------------------------------- INTC_API struct intc_u256_string INTC_API_PREFIX(256_str)(struct intc_u256 in, unsigned base, int separate_every_n_chars, char separate_ch) { - struct intc_u256_string val = {0}; + struct intc_u256_string val = INTC_ZERO_INIT; if ((base < 2) || (base > 36)) return val; @@ -1840,7 +1840,7 @@ INTC_API struct intc_u256_string INTC_API_PREFIX(256_str)(struct intc_u256 in, u } while (INTC_API_PREFIX(256_as_bool)(div_result.quot)); } - INTC_ASSERT(val.size <= sizeof(val.str) - 1); + INTC_ASSERT(val.size <= (int)(sizeof(val.str) - 1)); struct intc_u256_string result; result.size = 0; diff --git a/intc_tests.c b/intc_tests.c index ba853f4..b90f4ac 100644 --- a/intc_tests.c +++ b/intc_tests.c @@ -134,12 +134,9 @@ intc_u64 const INTC_TESTS_MAX_UNSIGNED_VALUES[] = { char const *file = intc_strip_path_to_file(__FILE__); \ printf(" +--Test failed at %s:%d, expression was: %s\n", file, __LINE__, #expr); \ printf(" V\n"); \ - if (fmt) \ - { \ - printf(" |--"); \ - printf(fmt, __VA_ARGS__); \ - fputc('\n', stdout); \ - } \ + printf(" |--"); \ + printf(fmt, __VA_ARGS__); \ + fputc('\n', stdout); \ } \ } while (0) @@ -199,7 +196,7 @@ intc_u64 const INTC_TESTS_MAX_UNSIGNED_VALUES[] = { // ----------------------------------------------------------------------------- static char *intc_strip_path_to_file(char const *file) { - int size = strlen(file); + int size = (int)strlen(file); char *result = (char *)file; for (int i = size - 1; i >= 0; i--) { @@ -215,7 +212,7 @@ static char *intc_strip_path_to_file(char const *file) struct intc_test_state intc_u128_unit_tests(void) { - struct intc_test_state result = {0}; + struct intc_test_state result = INTC_ZERO_INIT; printf(INTC_TESTS_COLOR_MAGENTA "intc_u128 unit tests" INTC_TESTS_COLOR_RESET); printf("\n accessors.cpp\n"); { @@ -420,8 +417,8 @@ struct intc_test_state intc_u128_unit_tests(void) for (int lo = 0; lo < 2; lo++) { struct intc_u128 const val = INTC_U128((intc_u64)lo, (intc_u64)hi); - INTC_TESTS_ASSERT(val.hi == hi); - INTC_TESTS_ASSERT(val.lo == lo); + INTC_TESTS_ASSERT(val.hi == (intc_u64)hi); + INTC_TESTS_ASSERT(val.lo == (intc_u64)lo); } } @@ -454,7 +451,6 @@ struct intc_test_state intc_u128_unit_tests(void) INTC_TESTS_BEGIN("Arithmetic.divide"); struct intc_u128 const big_val = INTC_U64_TO_U128(0xfedbca9876543210ULL); struct intc_u128 const small_val = INTC_U64_TO_U128(0xffffULL); - struct intc_u128 const res_val = INTC_U64_TO_U128(0xfedcc9753fc9ULL); INTC_TESTS_ASSERT(intc_u128_eq(intc_u128_div(small_val, small_val), INTC_U64_TO_U128(1))); INTC_TESTS_ASSERT(intc_u128_eq(intc_u128_div(small_val, big_val), INTC_U64_TO_U128(0))); @@ -521,11 +517,9 @@ struct intc_test_state intc_u128_unit_tests(void) { { INTC_TESTS_BEGIN("Function.str"); - int const leading_zeros = 5; // number of leading 0s - // make sure all of the test strings create the ASCII version of the string struct intc_u128 const original = INTC_U64_TO_U128(2216002924); - for (int test_index = 0; test_index < sizeof(INTC_TESTS_STRING_BASE_TESTS)/sizeof(INTC_TESTS_STRING_BASE_TESTS[0]); test_index++) + for (int test_index = 0; test_index < (int)(sizeof(INTC_TESTS_STRING_BASE_TESTS)/sizeof(INTC_TESTS_STRING_BASE_TESTS[0])); test_index++) { struct intc_base_to_string const *test_entry = INTC_TESTS_STRING_BASE_TESTS + test_index; struct intc_u128_string output = intc_u128_str(original, test_entry->base, 0 /*separate_every_n_chars*/, ' ' /*separate_ch*/); @@ -557,7 +551,7 @@ struct intc_test_state intc_u128_unit_tests(void) { INTC_TESTS_BEGIN("External.greater_than"); for (int index = 0; - index < sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0]); + index < (int)(sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0])); index++) { struct intc_u128 const small = INTC_U128_ZERO; @@ -591,7 +585,7 @@ struct intc_test_state intc_u128_unit_tests(void) { INTC_TESTS_BEGIN("External.greater_than_or_equals"); for (int index = 0; - index < sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0]); + index < (int)(sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0])); index++) { struct intc_u128 const small = INTC_U128_ZERO; @@ -637,13 +631,10 @@ struct intc_test_state intc_u128_unit_tests(void) { INTC_TESTS_BEGIN("External.shift_left"); - bool t = true; - bool f = false; intc_u8 u8 = 0xffULL; intc_u16 u16 = 0xffffULL; intc_u32 u32 = 0xffffffff; intc_u64 u64 = 0xffffffffffffffff; - struct intc_u128 const u128 = INTC_U128(0xffffffffffffffffULL, 0xffffffffffffffffULL); INTC_TESTS_ASSERT(intc_u128_eq(intc_u128_lshift(INTC_U64_TO_U128(u8), 7), INTC_U64_TO_U128(0x7f80ULL))); INTC_TESTS_ASSERT(intc_u128_eq(intc_u128_lshift(INTC_U64_TO_U128(u16), 15), INTC_U64_TO_U128(0x7fff8000ULL))); @@ -671,7 +662,7 @@ struct intc_test_state intc_u128_unit_tests(void) { INTC_TESTS_BEGIN("External.less_than"); for (int index = 0; - index < sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0]); + index < (int)(sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0])); index++) { struct intc_u128 const small = INTC_U128_ZERO; @@ -705,7 +696,7 @@ struct intc_test_state intc_u128_unit_tests(void) { INTC_TESTS_BEGIN("External.less_than_or_equals"); for (int index = 0; - index < sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0]); + index < (int)(sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0])); index++) { struct intc_u128 const small = INTC_U128_ZERO; @@ -749,7 +740,6 @@ struct intc_test_state intc_u128_unit_tests(void) intc_u16 u16 = 0xaaaaULL; intc_u32 u32 = 0xaaaaaaaaULL; intc_u64 u64 = 0xaaaaaaaaaaaaaaaaULL; - struct intc_u128 const u128 = INTC_U128(0xaaaaaaaaaaaaaaaaULL, 0xaaaaaaaaaaaaaaaaULL); struct intc_u128 const val = INTC_U64_TO_U128(0xd03ULL); // prime INTC_TESTS_ASSERT(intc_u128_eq(intc_u128_mod(INTC_U64_TO_U128(t), val), INTC_U64_TO_U128(true))); @@ -1030,7 +1020,7 @@ struct intc_test_state intc_u128_unit_tests(void) #if !defined(INTC_NO_U256) struct intc_test_state intc_u256_unit_tests(void) { - struct intc_test_state result = {0}; + struct intc_test_state result = INTC_ZERO_INIT; printf(INTC_TESTS_COLOR_MAGENTA "intc_u256 unit tests" INTC_TESTS_COLOR_RESET); printf("\n accessors.cpp\n"); { @@ -1329,7 +1319,6 @@ struct intc_test_state intc_u256_unit_tests(void) INTC_TESTS_BEGIN("Arithmetic.divide"); const struct intc_u256 big = INTC_U64_TO_U256(0xfedbca9876543210ULL); const struct intc_u256 small = INTC_U64_TO_U256(0xffffULL); - const struct intc_u256 res_val = INTC_U64_TO_U256(0xfedcc9753fc9ULL); INTC_TESTS_ASSERT(intc_u256_eq(intc_u256_div(small, small), INTC_U64_TO_U256(1))); INTC_TESTS_ASSERT(intc_u256_eq(intc_u256_div(small, big), INTC_U64_TO_U256(0))); @@ -1395,11 +1384,9 @@ struct intc_test_state intc_u256_unit_tests(void) { { INTC_TESTS_BEGIN("Function.str"); - int const leading_zeros = 5; // number of leading 0s - // make sure all of the test strings create the ASCII version of the string struct intc_u256 const original = INTC_U64_TO_U256(2216002924); - for (int test_index = 0; test_index < sizeof(INTC_TESTS_STRING_BASE_TESTS)/sizeof(INTC_TESTS_STRING_BASE_TESTS[0]); test_index++) + for (int test_index = 0; test_index < (int)(sizeof(INTC_TESTS_STRING_BASE_TESTS)/sizeof(INTC_TESTS_STRING_BASE_TESTS[0])); test_index++) { struct intc_base_to_string const *test_entry = INTC_TESTS_STRING_BASE_TESTS + test_index; struct intc_u256_string output = intc_u256_str(original, test_entry->base, 0 /*separate_every_n_chars*/, ' ' /*separate_ch*/); @@ -1431,7 +1418,7 @@ struct intc_test_state intc_u256_unit_tests(void) { INTC_TESTS_BEGIN("External.greater_than"); for (int index = 0; - index < sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0]); + index < (int)(sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0])); index++) { struct intc_u256 const small = INTC_U256_ZERO; @@ -1466,7 +1453,7 @@ struct intc_test_state intc_u256_unit_tests(void) { INTC_TESTS_BEGIN("External.greater_than_or_equals"); for (int index = 0; - index < sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0]); + index < (int)(sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0])); index++) { struct intc_u256 const small = INTC_U256_ZERO; @@ -1499,10 +1486,10 @@ struct intc_test_state intc_u256_unit_tests(void) INTC_U128(lo_lo ? 0xffffffffffffffffULL : 0x0000000000000000ULL, lo_hi ? 0xffffffffffffffffULL : 0x0000000000000000ULL), INTC_U128(hi_lo ? 0xffffffffffffffffULL : 0x0000000000000000ULL, hi_hi ? 0xffffffffffffffffULL : 0x0000000000000000ULL))); - INTC_TESTS_ASSERT(val.hi.hi == hi_hi ? 0x0000000000000000ULL : 0xffffffffffffffffULL); - INTC_TESTS_ASSERT(val.hi.lo == hi_lo ? 0x0000000000000000ULL : 0xffffffffffffffffULL); - INTC_TESTS_ASSERT(val.lo.hi == lo_hi ? 0x0000000000000000ULL : 0xffffffffffffffffULL); - INTC_TESTS_ASSERT(val.lo.lo == lo_lo ? 0x0000000000000000ULL : 0xffffffffffffffffULL); + INTC_TESTS_ASSERT(val.hi.hi == (intc_u64)hi_hi ? 0x0000000000000000ULL : 0xffffffffffffffffULL); + INTC_TESTS_ASSERT(val.hi.lo == (intc_u64)hi_lo ? 0x0000000000000000ULL : 0xffffffffffffffffULL); + INTC_TESTS_ASSERT(val.lo.hi == (intc_u64)lo_hi ? 0x0000000000000000ULL : 0xffffffffffffffffULL); + INTC_TESTS_ASSERT(val.lo.lo == (intc_u64)lo_lo ? 0x0000000000000000ULL : 0xffffffffffffffffULL); } } } @@ -1530,8 +1517,6 @@ struct intc_test_state intc_u256_unit_tests(void) { INTC_TESTS_BEGIN("External.shift_left"); - bool t = true; - bool f = false; intc_u8 u8 = 0xffULL; intc_u16 u16 = 0xffffULL; intc_u32 u32 = 0xffffffffULL; @@ -1565,7 +1550,7 @@ struct intc_test_state intc_u256_unit_tests(void) { INTC_TESTS_BEGIN("External.less_than"); for (int index = 0; - index < sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0]); + index < (int)(sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0])); index++) { struct intc_u256 const small = INTC_U256_ZERO; @@ -1599,7 +1584,7 @@ struct intc_test_state intc_u256_unit_tests(void) { INTC_TESTS_BEGIN("External.less_than_or_equals"); for (int index = 0; - index < sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0]); + index < (int)(sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES)/sizeof(INTC_TESTS_MAX_UNSIGNED_VALUES[0])); index++) { struct intc_u256 const small = INTC_U256_ZERO;