From cf6aa9653920918abf7ab1d002fdac3c70f03aad Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 28 Apr 2024 17:35:46 +0530 Subject: [PATCH] Wrote tests for lib/base.h's convert_bytes_to_* functions Just runs a sample of suitably byte arrays with an expected set of functions. The samples are in little endian format and the outputs are what we expect them to be. This should run regardless of the endian of your machine. --- test/lib/main.c | 3 +- test/lib/test-base.h | 67 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 test/lib/test-base.h diff --git a/test/lib/main.c b/test/lib/main.c index ffed775..d7529c7 100644 --- a/test/lib/main.c +++ b/test/lib/main.c @@ -10,9 +10,10 @@ * Description: */ -#include +#include "./test-base.h" int main(void) { + RUN_TEST_SUITE(test_lib); return 0; } diff --git a/test/lib/test-base.h b/test/lib/test-base.h new file mode 100644 index 0000000..08e8d3e --- /dev/null +++ b/test/lib/test-base.h @@ -0,0 +1,67 @@ +/* Copyright (C) 2024 Aryadev Chavali + + * You may distribute and modify this code under the terms of the + * GPLv2 license. You should have received a copy of the GPLv2 + * license with this file. If not, please write to: + * aryadev@aryadevchavali.com. + + * Created: 2024-04-28 + * Author: Aryadev Chavali + * Description: Tests for base.h + */ + +#ifndef TEST_BASE_H +#define TEST_BASE_H + +#include +#include + +void testing_lib_bytes_to_hword(void) +{ + byte_t tests[][4] = {{0, 0, 0, 0}, + {0xFF, 0xFF, 0xFF, 0xFF}, + {1}, + {0, 0, 0, 0b10000000}, + {0x89, 0xab, 0xcd, 0xef}}; + + hword_t expected[ARR_SIZE(tests)] = {0, HWORD_MAX, 1, 1 << 31, 0xefcdab89}; + + for (size_t i = 0; i < ARR_SIZE(tests); ++i) + { + hword_t got = convert_bytes_to_hword(tests[i]); + if (expected[i] != got) + { + FAIL(__func__, "[%lu] -> Expected 0x%x got 0x%x\n", i, expected[i], got); + assert(false); + } + } + SUCCESS(__func__, "%s\n", "Test succeeded"); +} + +void testing_lib_bytes_to_word(void) +{ + byte_t tests[][8] = {{0, 0, 0, 0}, + {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, + {0x01, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0b10000000}, + {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}}; + + word_t expected[ARR_SIZE(tests)] = {0, WORD_MAX, 1, 1LU << 63, + + 0xefcdab8967452301}; + for (size_t i = 0; i < ARR_SIZE(tests); ++i) + { + word_t got = convert_bytes_to_word(tests[i]); + if (expected[i] != got) + { + FAIL(__func__, "[%lu] -> Expected 0x%lx got 0x%lx\n", i, expected[i], + got); + assert(false); + } + } + SUCCESS(__func__, "%s\n", "Test succeeded"); +} + +TEST_SUITE(test_lib, testing_lib_bytes_to_hword, testing_lib_bytes_to_word); + +#endif