Skip to content

Commit

Permalink
tests/lone/types: add bytes read/write tests
Browse files Browse the repository at this point in the history
Verify that it is possible to read values from bytes structures
at arbitrary offsets regardless of alignment.
  • Loading branch information
matheusmoreira committed Jul 7, 2024
1 parent 2e9cf66 commit 892622e
Showing 1 changed file with 146 additions and 0 deletions.
146 changes: 146 additions & 0 deletions source/tests/lone/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,111 @@ LONE_TYPES_TEST_WRITE_UNALIGNED(s64, -1234567891011121314)
#undef LONE_TYPES_TEST_WRITE_ALIGNED
#undef LONE_TYPES_TEST_WRITE_UNALIGNED

#define LONE_TYPES_BYTES_TEST_NAME(type, operation, alignment) \
"lone/types/bytes/" #operation "/" #type "/" #alignment

#define LONE_TYPES_BYTES_TEST_FUNCTION(type, operation, alignment) \
test_lone_types_lone_bytes_##operation##_##type##_##alignment

#define LONE_TYPES_BYTES_TEST_READ_ALIGNED(type, constant) \
static enum lone_test_result LONE_TYPES_BYTES_TEST_FUNCTION(type, read, aligned)(void *context) \
{ \
lone_##type values[] = { 0, 0, constant, 0, 0 }; \
struct lone_bytes bytes = { sizeof(values), (unsigned char *) &values }; \
struct lone_##type actual = { false, 0 }; \
\
actual = lone_bytes_read_##type(bytes, 2 * sizeof(lone_##type)); \
\
return \
actual.present && actual.value == constant ? \
LONE_TEST_RESULT_PASS : LONE_TEST_RESULT_FAIL; \
}

#define LONE_TYPES_BYTES_TEST_READ_UNALIGNED(type, constant) \
static enum lone_test_result LONE_TYPES_BYTES_TEST_FUNCTION(type, read, unaligned)(void *context) \
{ \
struct __attribute__((packed)) { \
unsigned char byte; \
lone_##type value; \
} values[] = { {0}, {0}, { 0, constant }, {0}, {0} }; \
struct lone_bytes bytes = { sizeof(values), (unsigned char *) &values }; \
struct lone_##type actual = { false, 0 }; \
\
actual = lone_bytes_read_##type(bytes, 2 * sizeof(lone_##type) + 3); \
\
return \
actual.present && actual.value == constant ? \
LONE_TEST_RESULT_PASS : LONE_TEST_RESULT_FAIL; \
}

#define LONE_TYPES_BYTES_TEST_WRITE_ALIGNED(type, constant) \
static enum lone_test_result LONE_TYPES_BYTES_TEST_FUNCTION(type, write, aligned)(void *context) \
{ \
lone_##type values[] = { 0, 0, constant, 0, 0 }; \
struct lone_bytes bytes = { sizeof(values), (unsigned char *) &values }; \
bool written = false; \
\
written = lone_bytes_write_##type(bytes, 2 * sizeof(lone_##type), constant); \
\
return \
written && values[2] == constant ? \
LONE_TEST_RESULT_PASS : LONE_TEST_RESULT_FAIL; \
}

#define LONE_TYPES_BYTES_TEST_WRITE_UNALIGNED(type, constant) \
static enum lone_test_result LONE_TYPES_BYTES_TEST_FUNCTION(type, write, unaligned)(void *context) \
{ \
struct __attribute__((packed)) { \
unsigned char byte; \
lone_##type value; \
} values[] = { {0}, {0}, {0}, {0}, {0} }; \
struct lone_bytes bytes = { sizeof(values), (unsigned char *) &values }; \
bool written = false; \
\
written = lone_bytes_write_##type(bytes, 2 * sizeof(lone_##type) + 3, constant); \
\
return \
written && values[2].value == constant ? \
LONE_TEST_RESULT_PASS : LONE_TEST_RESULT_FAIL; \
}

LONE_TYPES_BYTES_TEST_READ_ALIGNED(u8, +214)
LONE_TYPES_BYTES_TEST_READ_ALIGNED(s8, -42)
LONE_TYPES_BYTES_TEST_READ_ALIGNED(u16, +56535)
LONE_TYPES_BYTES_TEST_READ_ALIGNED(s16, -9001)
LONE_TYPES_BYTES_TEST_READ_ALIGNED(u32, +4294927296)
LONE_TYPES_BYTES_TEST_READ_ALIGNED(s32, -40000)
LONE_TYPES_BYTES_TEST_READ_ALIGNED(u64, +17212176182698430302UL)
LONE_TYPES_BYTES_TEST_READ_ALIGNED(s64, -1234567891011121314)

LONE_TYPES_BYTES_TEST_READ_UNALIGNED(u16, +56535)
LONE_TYPES_BYTES_TEST_READ_UNALIGNED(s16, -9001)
LONE_TYPES_BYTES_TEST_READ_UNALIGNED(u32, +4294927296)
LONE_TYPES_BYTES_TEST_READ_UNALIGNED(s32, -40000)
LONE_TYPES_BYTES_TEST_READ_UNALIGNED(u64, +17212176182698430302UL)
LONE_TYPES_BYTES_TEST_READ_UNALIGNED(s64, -1234567891011121314)

LONE_TYPES_BYTES_TEST_WRITE_ALIGNED(u8, +214)
LONE_TYPES_BYTES_TEST_WRITE_ALIGNED(s8, -42)
LONE_TYPES_BYTES_TEST_WRITE_ALIGNED(u16, +56535)
LONE_TYPES_BYTES_TEST_WRITE_ALIGNED(s16, -9001)
LONE_TYPES_BYTES_TEST_WRITE_ALIGNED(u32, +4294927296)
LONE_TYPES_BYTES_TEST_WRITE_ALIGNED(s32, -40000)
LONE_TYPES_BYTES_TEST_WRITE_ALIGNED(u64, +17212176182698430302UL)
LONE_TYPES_BYTES_TEST_WRITE_ALIGNED(s64, -1234567891011121314)

LONE_TYPES_BYTES_TEST_WRITE_UNALIGNED(u16, +56535)
LONE_TYPES_BYTES_TEST_WRITE_UNALIGNED(s16, -9001)
LONE_TYPES_BYTES_TEST_WRITE_UNALIGNED(u32, +4294927296)
LONE_TYPES_BYTES_TEST_WRITE_UNALIGNED(s32, -40000)
LONE_TYPES_BYTES_TEST_WRITE_UNALIGNED(u64, +17212176182698430302UL)
LONE_TYPES_BYTES_TEST_WRITE_UNALIGNED(s64, -1234567891011121314)

#undef LONE_TYPES_BYTES_TEST_READ_ALIGNED
#undef LONE_TYPES_BYTES_TEST_READ_UNALIGNED
#undef LONE_TYPES_BYTES_TEST_WRITE_ALIGNED
#undef LONE_TYPES_BYTES_TEST_WRITE_UNALIGNED

static void test_finished(struct lone_test_case *test, void *context)
{
struct lone_bytes result;
Expand Down Expand Up @@ -164,6 +269,44 @@ long lone(int argc, char **argv, char **envp, struct lone_auxiliary_vector *auxv

#undef LONE_TYPES_TEST_CASE

#define LONE_TYPES_BYTES_TEST_CASE(type, operation, alignment) \
LONE_TEST_CASE(LONE_TYPES_BYTES_TEST_NAME(type, operation, alignment), \
LONE_TYPES_BYTES_TEST_FUNCTION(type, operation, alignment))

LONE_TYPES_BYTES_TEST_CASE(u8, read, aligned),
LONE_TYPES_BYTES_TEST_CASE(s8, read, aligned),
LONE_TYPES_BYTES_TEST_CASE(u16, read, aligned),
LONE_TYPES_BYTES_TEST_CASE(s16, read, aligned),
LONE_TYPES_BYTES_TEST_CASE(u32, read, aligned),
LONE_TYPES_BYTES_TEST_CASE(s32, read, aligned),
LONE_TYPES_BYTES_TEST_CASE(u64, read, aligned),
LONE_TYPES_BYTES_TEST_CASE(s64, read, aligned),

LONE_TYPES_BYTES_TEST_CASE(u16, read, unaligned),
LONE_TYPES_BYTES_TEST_CASE(s16, read, unaligned),
LONE_TYPES_BYTES_TEST_CASE(u32, read, unaligned),
LONE_TYPES_BYTES_TEST_CASE(s32, read, unaligned),
LONE_TYPES_BYTES_TEST_CASE(u64, read, unaligned),
LONE_TYPES_BYTES_TEST_CASE(s64, read, unaligned),

LONE_TYPES_BYTES_TEST_CASE(u8, write, aligned),
LONE_TYPES_BYTES_TEST_CASE(s8, write, aligned),
LONE_TYPES_BYTES_TEST_CASE(u16, write, aligned),
LONE_TYPES_BYTES_TEST_CASE(s16, write, aligned),
LONE_TYPES_BYTES_TEST_CASE(u32, write, aligned),
LONE_TYPES_BYTES_TEST_CASE(s32, write, aligned),
LONE_TYPES_BYTES_TEST_CASE(u64, write, aligned),
LONE_TYPES_BYTES_TEST_CASE(s64, write, aligned),

LONE_TYPES_BYTES_TEST_CASE(u16, write, unaligned),
LONE_TYPES_BYTES_TEST_CASE(s16, write, unaligned),
LONE_TYPES_BYTES_TEST_CASE(u32, write, unaligned),
LONE_TYPES_BYTES_TEST_CASE(s32, write, unaligned),
LONE_TYPES_BYTES_TEST_CASE(u64, write, unaligned),
LONE_TYPES_BYTES_TEST_CASE(s64, write, unaligned),

#undef LONE_TYPES_BYTES_TEST_CASE

LONE_TEST_CASE_NULL(),
};

Expand All @@ -186,6 +329,9 @@ long lone(int argc, char **argv, char **envp, struct lone_auxiliary_vector *auxv
}
}

#undef LONE_TYPES_BYTES_TEST_NAME
#undef LONE_TYPES_BYTES_TEST_FUNCTION

#undef LONE_TYPES_TEST_NAME
#undef LONE_TYPES_TEST_FUNCTION

Expand Down

0 comments on commit 892622e

Please sign in to comment.