Skip to content

Commit 1bd61ac

Browse files
committed
Add shared library test
1 parent 1dcb645 commit 1bd61ac

File tree

6 files changed

+65
-11
lines changed

6 files changed

+65
-11
lines changed

Makefile

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
prefix := /usr/local
22
includedir := $(prefix)/include
33

4-
BUILD_DIR := build
5-
LIB_DIR := libs
6-
EXAMPLE_DIR := examples
7-
TEST_DIR := tests
8-
BASELINE_DIR := tests/baselines
4+
BUILD_DIR := build
5+
LIB_DIR := libs
6+
EXAMPLE_DIR := examples
7+
TEST_DIR := tests
8+
BASELINE_DIR := tests/baselines
9+
SHARED_LIB_DIR := tests/shared_lib
910

1011
# Test flags
1112
COMMON_FLAGS := -Wall -Wextra -Werror -pedantic -I . -DUPRINTF_TEST -fsanitize=undefined,address,leak -g2
@@ -116,17 +117,28 @@ $(EXAMPLE_DIR)/%.out: $(BUILD_DIR)/$(EXAMPLE_DIR)/%
116117
# ======================== Tests ==========================
117118

118119
test: tests
119-
tests: $(foreach O,$(O_LEVELS), \
120-
$(foreach C,$(C_COMPILERS),$(foreach T,$(C_TESTS),$(BUILD_DIR)/test/$T/$C-$O)) \
121-
$(foreach C,$(CXX_COMPILERS),$(foreach T,$(CXX_TESTS),$(BUILD_DIR)/test/$T/$C-$O)))
120+
tests: $(BUILD_DIR)/$(SHARED_LIB_DIR)/bin \
121+
$(foreach O,$(O_LEVELS), \
122+
$(foreach C,$(C_COMPILERS),$(foreach T,$(C_TESTS),$(BUILD_DIR)/tests/$T/$C-$O)) \
123+
$(foreach C,$(CXX_COMPILERS),$(foreach T,$(CXX_TESTS),$(BUILD_DIR)/tests/$T/$C-$O)))
124+
125+
$(BUILD_DIR)/$(SHARED_LIB_DIR)/bin: $(SHARED_LIB_DIR)/bin.c $(BUILD_DIR)/$(SHARED_LIB_DIR)/libtest.so
126+
@mkdir -p $(@D)
127+
$(CC) $(CFLAGS) $< -o $@ -L$(BUILD_DIR)/$(SHARED_LIB_DIR) -ltest -Wl,-rpath=$(BUILD_DIR)/$(SHARED_LIB_DIR)
128+
./$@ > $(BUILD_DIR)/$(SHARED_LIB_DIR)/out
129+
diff $(BUILD_DIR)/$(SHARED_LIB_DIR)/out $(SHARED_LIB_DIR)/baseline.out
130+
131+
$(BUILD_DIR)/$(SHARED_LIB_DIR)/libtest.so: $(SHARED_LIB_DIR)/lib.c
132+
@mkdir -p $(@D)
133+
$(CC) $(CFLAGS) -fPIC -shared $< -o $@
122134

123135
define C_TEST_TEMPLATE
124-
$(BUILD_DIR)/test/$1/$2-$3: $(TEST_DIR)/$1.c $(BUILD_DIR)/impl/$2.o
136+
$(BUILD_DIR)/tests/$1/$2-$3: $(TEST_DIR)/$1.c $(BUILD_DIR)/impl/$2.o
125137
@./test.sh $1 "$1-$2-$3" $2 "$(CFLAGS) -$3" $$^ $$@ $(BASELINE_DIR)/$1.out
126138
endef
127139

128140
define CXX_TEST_TEMPLATE
129-
$(BUILD_DIR)/test/$1/$2-$3: $(TEST_DIR)/$1.cc $(BUILD_DIR)/impl/$2.o
141+
$(BUILD_DIR)/tests/$1/$2-$3: $(TEST_DIR)/$1.cc $(BUILD_DIR)/impl/$2.o
130142
@./test.sh $1 "$1-$2-$3" $2 "$(CXXFLAGS) -$3" $$^ $$@ $(BASELINE_DIR)/$1.out
131143
endef
132144

tests/shared_lib/baseline.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Common: {
2+
int a = 1
3+
}
4+
LibPrivate: {
5+
int b = 2
6+
}
7+
BinPrivate: {} <opaque>

tests/shared_lib/bin.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdlib.h>
2+
#include "lib.h"
3+
4+
typedef struct BinPrivate {
5+
int c;
6+
} BinPrivate;
7+
8+
int main(void) {
9+
print_common(&(Common) {.a = 1});
10+
print_lib_private();
11+
print_bin_private(&(BinPrivate) {.c = 3});
12+
return EXIT_SUCCESS;
13+
}

tests/shared_lib/lib.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "lib.h"
2+
3+
#define UPRINTF_IMPLEMENTATION
4+
#include "uprintf.h"
5+
6+
typedef struct {
7+
int b;
8+
} LibPrivate;
9+
10+
void print_common(Common *s) { uprintf("Common: %S\n", s); }
11+
void print_lib_private() { uprintf("LibPrivate: %S\n", &(LibPrivate) {.b = 2}); }
12+
void print_bin_private(struct BinPrivate *s) { uprintf("BinPrivate: %S\n", s); }

tests/shared_lib/lib.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
typedef struct {
2+
int a;
3+
} Common;
4+
5+
struct BinPrivate;
6+
7+
void print_common(Common *s);
8+
void print_lib_private();
9+
void print_bin_private(struct BinPrivate *s);

uprintf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3746,7 +3746,8 @@ static bool _upf_get_this_object_cb(
37463746

37473747
_upf_get_this_object_cb_data *data = (_upf_get_this_object_cb_data *) raw_data;
37483748
*data->path = _upf_copy_string(path);
3749-
*data->address = (void *) (start - offset);
3749+
_UPF_ASSERT(offset < UINTPTR_MAX);
3750+
*data->address = (void *) (start - (uintptr_t) offset);
37503751
return true;
37513752
}
37523753

0 commit comments

Comments
 (0)