Add a self test that validates ref_get_offset_size()

Hopefully this can figure out what goes wrong with
https://github.com/flatpak/flatpak/issues/3503
This commit is contained in:
Alexander Larsson
2020-11-30 09:10:29 +01:00
parent 444b2b3217
commit 04abfc9e53
2 changed files with 56 additions and 1 deletions

View File

@@ -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

52
selftests.c Normal file
View File

@@ -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;
}