diff --git a/src/internal.h b/src/internal.h index b11186dd7f39c16d0e5485ce7beb111527e550ba..b35b7d5a3a22cb19bcfbcb341078070bda2bc93b 100644 --- a/src/internal.h +++ b/src/internal.h @@ -155,20 +155,20 @@ static inline void h_sarray_clear(HSArray *arr) { typedef unsigned int *HCharset; static inline HCharset new_charset(HAllocator* mm__) { - HCharset cs = h_new(unsigned int, 256 / sizeof(unsigned int)); - memset(cs, 0, 256); + HCharset cs = h_new(unsigned int, 256 / (sizeof(unsigned int) * 8)); + memset(cs, 0, 32); // 32 bytes = 256 bits return cs; } static inline int charset_isset(HCharset cs, uint8_t pos) { - return !!(cs[pos / sizeof(*cs)] & (1 << (pos % sizeof(*cs)))); + return !!(cs[pos / (sizeof(*cs)*8)] & (1 << (pos % (sizeof(*cs)*8)))); } static inline void charset_set(HCharset cs, uint8_t pos, int val) { - cs[pos / sizeof(*cs)] = + cs[pos / (sizeof(*cs)*8)] = val - ? cs[pos / sizeof(*cs)] | (1 << (pos % sizeof(*cs))) - : cs[pos / sizeof(*cs)] & ~(1 << (pos % sizeof(*cs))); + ? cs[pos / (sizeof(*cs)*8)] | (1 << (pos % (sizeof(*cs)*8))) + : cs[pos / (sizeof(*cs)*8)] & ~(1 << (pos % (sizeof(*cs)*8))); } typedef unsigned int HHashValue; diff --git a/src/t_regression.c b/src/t_regression.c index faa953c8451073e556acb2eda1f1fcf48ecc2d79..aa78f2c8c02ad218db7e89f0543c06ebdc3e7f25 100644 --- a/src/t_regression.c +++ b/src/t_regression.c @@ -151,6 +151,28 @@ static void test_cfg_many_seq(void) { // reshape on h_many. } +static uint8_t test_charset_bits__buf[256]; +static void *test_charset_bits__alloc(HAllocator *allocator, size_t size) +{ + g_check_cmp_uint64(size, ==, 256/8); + assert(size <= 256); + return test_charset_bits__buf; +} +static void test_charset_bits(void) { + // charset would allocate 256 bytes instead of 256 bits (= 32 bytes) + + HAllocator alloc = { + .alloc = test_charset_bits__alloc, + .realloc = NULL, + .free = NULL, + }; + test_charset_bits__buf[32] = 0xAB; + HCharset cs = new_charset(&alloc); + for(size_t i=0; i<32; i++) + g_check_cmp_uint32(test_charset_bits__buf[i], ==, 0); + g_check_cmp_uint32(test_charset_bits__buf[32], ==, 0xAB); +} + void register_regression_tests(void) { g_test_add_func("/core/regression/bug118", test_bug118); g_test_add_func("/core/regression/seq_index_path", test_seq_index_path); @@ -158,4 +180,5 @@ void register_regression_tests(void) { g_test_add_func("/core/regression/llk_zero_end", test_llk_zero_end); g_test_add_func("/core/regression/lalr_charset_lhs", test_lalr_charset_lhs); g_test_add_func("/core/regression/cfg_many_seq", test_cfg_many_seq); + g_test_add_func("/core/regression/charset_bits", test_charset_bits); }