stb icon indicating copy to clipboard operation
stb copied to clipboard

stb_ds.h unit test fails

Open awmorgan opened this issue 3 years ago • 2 comments

Describe the bug I ran the stbds_unit_test and it failed

To Reproduce Compile the test with this OS and compiler.

MacOS:

/Volumes/git/stb (master)*$ uname -v
Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64

Clang Compiler:

/Volumes/git/stb (master)*$ clang -v
Apple clang version 12.0.5 (clang-1205.0.22.11)
Target: x86_64-apple-darwin20.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

compilation command:

clang \
-g \
-Wall -Wextra -Wpedantic \
-Wno-unused-variable \
-Wno-unused-parameter \
-Wno-missing-braces \
-DDS_TEST \
tests/test_ds.c \
-o build/main

Run the test

./build/main

You will get a failed assertion:

Assertion failed: (hmgets(map3, s.key).d == i*5), function stbds_unit_tests, file tests/../stb_ds.h, line 1847.

Expected behavior Test passes.

Screenshots image

I also noticed some compiler warning coming from the ADDRESSOF macro that may be related:

tests/../stb_ds.h:1846:29: warning: incompatible pointer to integer conversion initializing 'int' with an expression of type 'int [2]' [-Wint-conversion]

full compilation output:

++ set -e
++ mkdir -p build
++ clang -g -Wall -Wextra -Wpedantic -Wno-unused-variable -Wno-unused-parameter -Wno-missing-braces -DDS_TEST tests/test_ds.c -o build/main
In file included from tests/test_ds.c:36:
tests/../stb_ds.h:1846:29: warning: incompatible pointer to integer conversion initializing 'int' with an expression of type 'int [2]' [-Wint-conversion]
    if (i & 1) STBDS_ASSERT(hmgets(map3, s.key).d == 0);
               ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
tests/../stb_ds.h:421:21: note: expanded from macro 'hmgets'
#define hmgets      stbds_hmgets
                    ^
tests/../stb_ds.h:598:33: note: expanded from macro 'stbds_hmgets'
#define stbds_hmgets(t, k)    (*stbds_hmgetp(t,k))
                                ^
tests/../stb_ds.h:581:13: note: expanded from macro 'stbds_hmgetp'
    ((void) stbds_hmgeti(t,k), &(t)[stbds_temp((t)-1)])
            ^
tests/../stb_ds.h:573:88: note: expanded from macro 'stbds_hmgeti'
    ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_HM_BINARY), \
                                                                                       ^
tests/../stb_ds.h:524:73: note: expanded from macro 'STBDS_ADDRESSOF'
  #define STBDS_ADDRESSOF(typevar, value)     ((__typeof__(typevar)[1]){value}) // literal array decays to pointer to value
                                                                        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/assert.h:93:25: note: expanded from macro 'assert'
    (__builtin_expect(!(e), 0) ? __assert_rtn(__func__, __FILE__, __LINE__, #e) : (void)0)
                        ^
In file included from tests/test_ds.c:36:
tests/../stb_ds.h:1847:29: warning: incompatible pointer to integer conversion initializing 'int' with an expression of type 'int [2]' [-Wint-conversion]
    else       STBDS_ASSERT(hmgets(map3, s.key).d == i*5);
               ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tests/../stb_ds.h:421:21: note: expanded from macro 'hmgets'
#define hmgets      stbds_hmgets
                    ^
tests/../stb_ds.h:598:33: note: expanded from macro 'stbds_hmgets'
#define stbds_hmgets(t, k)    (*stbds_hmgetp(t,k))
                                ^
tests/../stb_ds.h:581:13: note: expanded from macro 'stbds_hmgetp'
    ((void) stbds_hmgeti(t,k), &(t)[stbds_temp((t)-1)])
            ^
tests/../stb_ds.h:573:88: note: expanded from macro 'stbds_hmgeti'
    ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_HM_BINARY), \
                                                                                       ^
tests/../stb_ds.h:524:73: note: expanded from macro 'STBDS_ADDRESSOF'
  #define STBDS_ADDRESSOF(typevar, value)     ((__typeof__(typevar)[1]){value}) // literal array decays to pointer to value
                                                                        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/assert.h:93:25: note: expanded from macro 'assert'
    (__builtin_expect(!(e), 0) ? __assert_rtn(__func__, __FILE__, __LINE__, #e) : (void)0)
                        ^
2 warnings generated.
++ ./build/main
Assertion failed: (hmgets(map3, s.key).d == i*5), function stbds_unit_tests, file tests/../stb_ds.h, line 1847.
./make.sh: line 21: 95029 Abort trap: 6           ./build/main
The terminal process "bash '-c', './make.sh'" terminated with exit code: 134.

Terminal will be reused by tasks, press any key to close it.

The macros from lines 1846 and 1847 expand to this code:

    if (i & 1)
      (__builtin_expect(
           !((*((void)((map3) = stbds_hmget_key(
                           (map3), sizeof *(map3),
                           (void *)((__typeof__((map3)->key)[1]){(s.key)}),
                           sizeof(map3)->key, 0),
                       ((stbds_array_header *)((map3)-1) - 1)->temp),
                &(map3)[((stbds_array_header *)((map3)-1) - 1)->temp]))
                 .d == 0),
           0)
           ? __assert_rtn(__func__, "tests/../stb_ds.h", 1847,
                          "hmgets(map3, s.key).d == 0")
           : (void)0);
    else
      (__builtin_expect(
           !((*((void)((map3) = stbds_hmget_key(
                           (map3), sizeof *(map3),
                           (void *)((__typeof__((map3)->key)[1]){(s.key)}),
                           sizeof(map3)->key, 0),
                       ((stbds_array_header *)((map3)-1) - 1)->temp),
                &(map3)[((stbds_array_header *)((map3)-1) - 1)->temp]))
                 .d == i * 5),
           0)
           ? __assert_rtn(__func__, "tests/../stb_ds.h", 1848,
                          "hmgets(map3, s.key).d == i*5")
           : (void)0);

this portion of the code is generating the warning:

(void *)((__typeof__((map3)->key)[1]){(s.key)}),

awmorgan avatar Nov 21 '21 21:11 awmorgan

If I change the ADDRESSOF macro for clang to just do &(value) and comment out the test cases for gcc/clang that use rvalue keys then this test case that failed before passes.

awmorgan avatar Nov 22 '21 00:11 awmorgan

need to figure out a way to make that addressof rvalue macro work for keys that are arrays

awmorgan avatar Nov 22 '21 00:11 awmorgan