mirror of https://github.com/nirenjan/libx52.git
test: Add test cases for libx52util
This change adds a test suite for libx52util, testing all the positive cases where a character is added to the lookup table. For now, this test suite only verifies single character mappings, not double character mappings. A future commit will add test cases for characters not in the map.pull/61/head
parent
f43ba6b902
commit
e9f4e1b3a8
|
|
@ -21,6 +21,7 @@ pkgconfig_DATA += libx52util/libx52util.pc
|
|||
|
||||
# Autogenerated file that needs to be cleaned up
|
||||
CLEANFILES += libx52util/util_char_map.c
|
||||
|
||||
util_char_map_c_DEPENDS = \
|
||||
$(srcdir)/libx52util/x52_char_map_gen.py \
|
||||
$(srcdir)/libx52util/x52_char_map.cfg
|
||||
|
|
@ -28,6 +29,25 @@ util_char_map_c_DEPENDS = \
|
|||
libx52util/util_char_map.c: $(util_char_map_c_DEPENDS)
|
||||
$(AM_V_GEN) $(PYTHON) $(util_char_map_c_DEPENDS) $@
|
||||
|
||||
if HAVE_CMOCKA
|
||||
TESTS += libx52util-map-test
|
||||
|
||||
check_PROGRAMS += libx52util-map-test
|
||||
|
||||
CLEANFILES += libx52util/x52_map_test.c
|
||||
x52_map_test_c_DEPENDS = \
|
||||
$(srcdir)/libx52util/x52_map_test_gen.py \
|
||||
$(srcdir)/libx52util/x52_char_map.cfg
|
||||
|
||||
libx52util/x52_map_test.c: $(x52_map_test_c_DEPENDS)
|
||||
$(AM_V_GEN) $(PYTHON) $(x52_map_test_c_DEPENDS) $@
|
||||
|
||||
libx52util_map_test_SOURCES = libx52util/x52_map_test.c
|
||||
libx52util_map_test_CFLAGS = @CMOCKA_CFLAGS@ -I $(top_srcdir) -I $(top_srcdir)/libx52util
|
||||
libx52util_map_test_LDFLAGS = @CMOCKA_LIBS@
|
||||
libx52util_map_test_LDADD = libx52util.la
|
||||
endif
|
||||
|
||||
# Extra files that need to be in the distribution
|
||||
EXTRA_DIST += libx52util/x52_char_map.cfg \
|
||||
libx52util/x52_char_map.h \
|
||||
|
|
|
|||
|
|
@ -0,0 +1,107 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Generate a test script for the convert function"""
|
||||
|
||||
import argparse
|
||||
import re
|
||||
|
||||
def parse_file(map_file):
|
||||
"""Read the map file, strip out comments, and return a dictionary that
|
||||
maps the UTF-8 encoded string to the X52 MFD character"""
|
||||
|
||||
# If we are running this, then we know that the input map is likely
|
||||
# in a sane format already.
|
||||
char_dict = {}
|
||||
|
||||
with open(map_file, 'r', encoding='utf-8') as map_fd:
|
||||
for line in map_fd:
|
||||
line = re.sub(r'#.*$', '', line).strip()
|
||||
|
||||
if not line:
|
||||
# Comment line, skip
|
||||
continue
|
||||
|
||||
key, out = line.split()
|
||||
in_char = int(key, 0)
|
||||
|
||||
if len(out) == 1:
|
||||
out_byte = ord(out)
|
||||
else:
|
||||
out_byte = int(out, 0)
|
||||
|
||||
char_dict[in_char] = out_byte
|
||||
|
||||
return char_dict
|
||||
|
||||
def generate_positive_test_cases(char_dict):
|
||||
"""Generate a set of positive test cases"""
|
||||
# For every string in the dictionary, generate a test case that tests
|
||||
# the input against the output
|
||||
TEST_CASE_FMT = """
|
||||
static void test_map_{in_char}(void **state) {{
|
||||
(void)state;
|
||||
const uint8_t input_array[] = {{ {in_bytes}, 0 }};
|
||||
const uint8_t expected_output[2] = {{ {out_byte}, 0 }};
|
||||
size_t out_len = 20;
|
||||
uint8_t output[20] = {{ 0 }};
|
||||
int rc;
|
||||
|
||||
rc = libx52util_convert_utf8_string(input_array, output, &out_len);
|
||||
assert_int_equal(rc, 0);
|
||||
assert_int_equal(out_len, 1);
|
||||
assert_string_equal(output, expected_output);
|
||||
}}
|
||||
"""
|
||||
|
||||
output = ""
|
||||
for in_char, out_byte in char_dict.items():
|
||||
in_bytes = ", ".join(hex(c) for c in chr(in_char).encode('utf-8'))
|
||||
in_tc = hex(in_char)
|
||||
|
||||
output += TEST_CASE_FMT.format(in_char=in_tc, in_bytes=in_bytes, out_byte=out_byte)
|
||||
|
||||
output += """
|
||||
const struct CMUnitTest tests[] = {
|
||||
"""
|
||||
|
||||
for in_char in sorted(char_dict.keys()):
|
||||
output += f" cmocka_unit_test(test_map_{hex(in_char)}),\n"
|
||||
|
||||
output += '};\n'
|
||||
|
||||
return output
|
||||
|
||||
TEST_HEADER = """
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdarg.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "libx52util.h"
|
||||
"""
|
||||
|
||||
TEST_FOOTER = """
|
||||
int main(void) {
|
||||
cmocka_set_message_output(CM_OUTPUT_TAP);
|
||||
cmocka_run_group_tests(tests, NULL, NULL);
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
def main():
|
||||
"""Generate X52 map test suite"""
|
||||
parser = argparse.ArgumentParser(description='Generate map test cases')
|
||||
parser.add_argument('INPUT_FILE', help="Input character map file")
|
||||
parser.add_argument('OUTPUT_FILE', help="Generated test script")
|
||||
args = parser.parse_args()
|
||||
|
||||
char_dict = parse_file(args.INPUT_FILE)
|
||||
test_cases = generate_positive_test_cases(char_dict)
|
||||
|
||||
with open(args.OUTPUT_FILE, 'w', encoding='utf-8') as out_fd:
|
||||
print(TEST_HEADER, file=out_fd)
|
||||
print(test_cases, file=out_fd)
|
||||
print(TEST_FOOTER, file=out_fd)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Reference in New Issue