diff --git a/Makefile b/Makefile index 7047b4fb..418b190f 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ PKG_CONFIG ?= pkg-config GLIB_CFLAGS = `$(PKG_CONFIG) --cflags glib-2.0` GLIB_LIBS = `$(PKG_CONFIG) --libs glib-2.0` -all: sample ostree_test performance +all: sample ostree_test performance selftests sample.h: variant-schema-compiler sample.gv ./variant-schema-compiler --internal-validation --outfile sample-impl.h --outfile-header=sample.h --prefix=sample sample.gv @@ -25,6 +25,9 @@ ostree_test.h: variant-schema-compiler ostree.gv ostree_test: ostree_test.c ostree_test.h $(CC) $(GLIB_CFLAGS) $(CFLAGS) -o ostree_test ostree_test.c $(GLIB_LIBS) +selftests: selftests.c ostree_test.h + $(CC) $(GLIB_CFLAGS) $(CFLAGS) -o selftests -O2 selftests.c $(GLIB_LIBS) + clean: rm -f sample.h sample performance.h performance ostree_test.h ostree_test diff --git a/selftests.c b/selftests.c new file mode 100644 index 00000000..b38061ad --- /dev/null +++ b/selftests.c @@ -0,0 +1,52 @@ +#include "ostree_test.h" + + +static guint +slow_get_offset_size (gsize size) +{ + if (size > G_MAXUINT16) + { + if (size > G_MAXUINT32) + return 8; + else + return 4; + } + else + { + if (size > G_MAXUINT8) + return 2; + else + return 1; + } +} + +int +main (int argc, + char *argv[]) +{ + g_print ("Validating ot_ref_get_offset_size up to G_MAXUINT32\n"); + for (gsize i = 1; i < G_MAXUINT32; i++) + { + guint res = ot_ref_get_offset_size (i); + if (res != slow_get_offset_size (i)) + { + g_print ("failed: ot_ref_get_offset_size (%"G_GSIZE_FORMAT") == %d, should be %d\n", i, res, slow_get_offset_size (i)); + exit (1); + } + } + +#if GLIB_SIZEOF_SIZE_T > 4 + g_print ("Validating ot_ref_get_offset_size up to 2*G_MAXUINT32\n"); + for (gsize i = (gsize)G_MAXUINT32; i < (gsize)G_MAXUINT32 * 2; i++) + { + guint res = ot_ref_get_offset_size (i); + if (res != slow_get_offset_size (i)) + { + g_print ("failed: ot_ref_get_offset_size (%"G_GSIZE_FORMAT") == %d, should be %d\n", i, res, slow_get_offset_size (i)); + exit (1); + } + } +#endif + + return 0; +}