diff --git a/test.sh b/test.sh index cacdbf8..6398573 100755 --- a/test.sh +++ b/test.sh @@ -29,13 +29,15 @@ bin="$dir/$compiler-$o_level-$g_level" log="$bin.log" output="$bin.out" baseline="$BASELINE_DIR/$test.out" +is_gcc=false +if [ $(echo "$compiler" | head -c 3) = "gcc" ]; then is_gcc=true; fi # Some tests don't work on older versions of compilers function should_skip { result=false - if [ $(echo "$compiler" | head -c 5) = "clang" ]; then + if [ $is_gcc = false ]; then major_version=$("$compiler" -dumpversion | cut -d. -f1) if [ $major_version -le 15 ]; then @@ -58,17 +60,25 @@ function uses_shared_implementation { # Some tests may not match baseline exactly, so they get custom target similarity percentage. function get_similarity { - # These tests contain stdint.h types which have different typenames in gcc and clang - if [ "$test" = "struct" ]; then echo 90; - elif [ "$test" = "packed" ]; then echo 90; + result=100 + + if [ $is_gcc = false ]; then + # These tests contain stdint.h types which have different typenames in gcc and clang. + # Baselines are generated from gcc, so decrease its similarity for clang: + if [ "$test" = "struct" ]; then result=90; + elif [ "$test" = "packed_struct" ]; then result=90; + elif [ "$test" = "primitives" ]; then result=95; + # Clang doesn't produce DW_AT_subprogram for external functions, i.e. shared libraries + # or different CUs, so cross-CU retrieval of function signature/name doesn't work. + elif [ "$test" = "function" ]; then result=85; fi + fi + # FILE has different implementation which is up to stdio.h and it often has pointers # that point out-of-bounds causing uprintf to print garbage from the memory, thus the # primary goal of the test is to check that there are no errors, i.e. segfaults, leaks. - elif [ "$test" = "stdio_file" ]; then echo 1; - # Clang doesn't produce DW_AT_subprogram for external functions, i.e. shared libraries - # or different CUs, so cross-CU retrieval of function signature/name doesn't work. - elif [ "$test" = "function" ]; then echo 80; - else echo 100; fi + if [ "$test" = "stdio_file" ]; then result=10; fi + + echo $result } diff --git a/tests/baselines/format.out b/tests/baselines/format_string.out similarity index 50% rename from tests/baselines/format.out rename to tests/baselines/format_string.out index e426a61..c2c5a35 100644 --- a/tests/baselines/format.out +++ b/tests/baselines/format_string.out @@ -1,6 +1,6 @@ Different format specifiers: 100 100 100 100 100 100 100 100 100 Escaped format specifier: 100 % -(uprintf) [ERROR] Unfinished format specifier at the end of the line at tests/format.c:14. -(uprintf) [ERROR] Unfinished format specifier at the end of the line at tests/format.c:18. -(uprintf) [ERROR] Unknown format specifier "%3" at tests/format.c:22. -(uprintf) [ERROR] Unknown format specifier "%-" at tests/format.c:26. +(uprintf) [ERROR] Unfinished format specifier at the end of the line at tests/format_string.c:14. +(uprintf) [ERROR] Unfinished format specifier at the end of the line at tests/format_string.c:18. +(uprintf) [ERROR] Unknown format specifier "%3" at tests/format_string.c:22. +(uprintf) [ERROR] Unknown format specifier "%-" at tests/format_string.c:26. diff --git a/tests/baselines/packed.out b/tests/baselines/packed_struct.out similarity index 92% rename from tests/baselines/packed.out rename to tests/baselines/packed_struct.out index 7294349..76bc922 100644 --- a/tests/baselines/packed.out +++ b/tests/baselines/packed_struct.out @@ -1,4 +1,4 @@ -Packed struct: { +Struct containing all types: { uint8_t u8 = 255 short unsigned int u16 = 65535 unsigned int u32 = 4294967295 @@ -14,7 +14,7 @@ Packed struct: { void *void_ptr = POINTER int *int_ptr = POINTER (5) float *null_ptr = NULL - char *ch_ptr = NULL + char *ch_ptr = POINTER ("c") const char *str = POINTER ("str string") char *const const_str = POINTER ("const_str string") _Bool b = true diff --git a/tests/baselines/primitives.out b/tests/baselines/primitives.out index 86d9aeb..7900283 100644 --- a/tests/baselines/primitives.out +++ b/tests/baselines/primitives.out @@ -11,6 +11,10 @@ f64: -0.321000 uch: 99 ('c') sch: 157 void_ptr: POINTER +int_ptr: POINTER (1) null_ptr: NULL -int_ptr: POINTER (123) -str: POINTER ("const char *str") +ch_ptr: POINTER ("c") +str: POINTER ("string") +b: false +void_fun: POINTER (void void_fun()) +other_fun: POINTER (int other_fun(char a, float *b, long unsigned int c)) diff --git a/tests/baselines/struct.out b/tests/baselines/struct.out index 2df88c5..76bc922 100644 --- a/tests/baselines/struct.out +++ b/tests/baselines/struct.out @@ -14,7 +14,7 @@ Struct containing all types: { void *void_ptr = POINTER int *int_ptr = POINTER (5) float *null_ptr = NULL - char *ch_ptr = NULL + char *ch_ptr = POINTER ("c") const char *str = POINTER ("str string") char *const const_str = POINTER ("const_str string") _Bool b = true diff --git a/tests/baselines/void.out b/tests/baselines/void_pointers.out similarity index 80% rename from tests/baselines/void.out rename to tests/baselines/void_pointers.out index bf08bb5..7941929 100644 --- a/tests/baselines/void.out +++ b/tests/baselines/void_pointers.out @@ -1,4 +1,4 @@ -(uprintf) [ERROR] Unable to print `void*` because it can point to arbitrary data of any length. To print the pointer itself, you must get pointer (&) of "v" at tests/void.c:8. +(uprintf) [ERROR] Unable to print `void*` because it can point to arbitrary data of any length. To print the pointer itself, you must get pointer (&) of "v" at tests/void_pointers.c:8. void* POINTER void** POINTER, void* POINTER void*** POINTER, void** POINTER diff --git a/tests/format.c b/tests/format_string.c similarity index 100% rename from tests/format.c rename to tests/format_string.c diff --git a/tests/packed.c b/tests/packed.c deleted file mode 100644 index 89c9e25..0000000 --- a/tests/packed.c +++ /dev/null @@ -1,87 +0,0 @@ -#include -#include -#include -#include "uprintf.h" - -typedef int8_t i8_t; -typedef char *cstr; -typedef double long_float; - -#pragma pack(push, 1) -typedef struct { - uint8_t u8; - unsigned short u16; - unsigned u32; - long unsigned long int u64; - - i8_t i8; - short int i16; - int32_t i32; - signed long int i64; - - signed char sch; - unsigned char uch; - - float f32; - long_float f64; - - void *void_ptr; - int *int_ptr; - float *null_ptr; - char *ch_ptr; - - const char *str; - const cstr const_str; - - bool b; - - void (*void_fun)(void); - int (*other_fun)(char, float *, unsigned long); -} Struct; -#pragma pack(pop) - -void void_fun(void) {} - -int other_fun(char a, float *b, unsigned long c) { - (void) a; - (void) b; - (void) c; - return 1; -} - -int main(void) { - int i = 5; - - Struct s = { - .u8 = 255, - .u16 = 65535, - .u32 = 4294967295, - .u64 = 18446744073709551615UL, - - .i8 = -128, - .i16 = -32768, - .i32 = -2147483648, - .i64 = -9223372036854775807L, - - .f32 = 0.123, - .f64 = -0.321, - - .uch = 'c', - .sch = -'c', - - .void_ptr = &i, - .int_ptr = &i, - .null_ptr = NULL, - .str = "str string", - .const_str = "const_str string", - - .b = true, - - .void_fun = &void_fun, - .other_fun = &other_fun, - }; - - uprintf("Packed struct: %S\n", &s); - - return _upf_test_status; -} diff --git a/tests/packed_struct.c b/tests/packed_struct.c new file mode 100644 index 0000000..083f02f --- /dev/null +++ b/tests/packed_struct.c @@ -0,0 +1,3 @@ +#pragma pack(push, 1) +#include "struct.c" +#pragma pack(pop) diff --git a/tests/primitives.c b/tests/primitives.c index eba55d4..ab14891 100644 --- a/tests/primitives.c +++ b/tests/primitives.c @@ -3,7 +3,19 @@ #include #include "uprintf.h" +void void_fun(void) {} + +int other_fun(char a, float *b, unsigned long c) { + (void) a; + (void) b; + (void) c; + return 1; +} + int main(void) { + int i = 1; + char c = 'c'; + uint8_t u8 = 255; uint16_t u16 = 65535; uint32_t u32 = 4294967295; @@ -20,11 +32,17 @@ int main(void) { signed char uch = 'c'; unsigned char sch = -'c'; - void *void_ptr = &i8; - void *null_ptr = NULL; - int i = 123; + void *void_ptr = &i; int *int_ptr = &i; - const char *str = "const char *str"; + float *null_ptr = NULL; + char *ch_ptr = &c; + + const char *str = "string"; + + bool b = false; + + void (*void_fun_v)(void) = &void_fun; + int (*other_fun_v)(char, float *, unsigned long) = &other_fun; uprintf("u8: %S\n", &u8); uprintf("u16: %S\n", &u16); @@ -43,9 +61,16 @@ int main(void) { uprintf("sch: %S\n", &sch); uprintf("void_ptr: %S\n", &void_ptr); - uprintf("null_ptr: %S\n", &null_ptr); uprintf("int_ptr: %S\n", &int_ptr); + uprintf("null_ptr: %S\n", &null_ptr); + uprintf("ch_ptr: %S\n", &ch_ptr); + uprintf("str: %S\n", &str); + uprintf("b: %S\n", &b); + + uprintf("void_fun: %S\n", &void_fun_v); + uprintf("other_fun: %S\n", &other_fun_v); + return _upf_test_status; } diff --git a/tests/struct.c b/tests/struct.c index 2c7f6ce..641e20b 100644 --- a/tests/struct.c +++ b/tests/struct.c @@ -50,6 +50,7 @@ int other_fun(char a, float *b, unsigned long c) { int main(void) { int i = 5; + char c = 'c'; Struct s = { .u8 = 255, @@ -71,6 +72,8 @@ int main(void) { .void_ptr = &i, .int_ptr = &i, .null_ptr = NULL, + .ch_ptr = &c, + .str = "str string", .const_str = "const_str string", diff --git a/tests/void.c b/tests/void_pointers.c similarity index 100% rename from tests/void.c rename to tests/void_pointers.c