From 420d545018dbb21956b4c441b42c7e4803d95a1d Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Sun, 28 Nov 2021 13:44:46 +0000 Subject: x1000-installer: tests for xf_stream.c Change-Id: Ib9d1ca0109029f09ff0f8adfc10925c7c9a0f8b9 --- lib/x1000-installer/Makefile | 3 +- lib/x1000-installer/test/data/lines_shuffled.txt | 108 +++++++++++++++++++++++ lib/x1000-installer/test/data/lines_sorted.txt | 36 ++++++++ lib/x1000-installer/test/main.c | 5 ++ lib/x1000-installer/test/test_stream.c | 108 +++++++++++++++++++++++ 5 files changed, 259 insertions(+), 1 deletion(-) create mode 100644 lib/x1000-installer/test/data/lines_shuffled.txt create mode 100644 lib/x1000-installer/test/data/lines_sorted.txt create mode 100644 lib/x1000-installer/test/test_stream.c diff --git a/lib/x1000-installer/Makefile b/lib/x1000-installer/Makefile index 1a63e6d5b3..b85fef31da 100644 --- a/lib/x1000-installer/Makefile +++ b/lib/x1000-installer/Makefile @@ -12,7 +12,8 @@ TOBJ = test_lib/core_alloc.o \ test_lib/pathfuncs.o \ test_lib/md5.o \ test_lib/strlcpy.o \ - test/main.o + test/main.o \ + test/test_stream.o TBIN = xf_test # dependency needs to be built manually diff --git a/lib/x1000-installer/test/data/lines_shuffled.txt b/lib/x1000-installer/test/data/lines_shuffled.txt new file mode 100644 index 0000000000..c138fe7e7c --- /dev/null +++ b/lib/x1000-installer/test/data/lines_shuffled.txt @@ -0,0 +1,108 @@ +1234567890abcdefghijklmnopqrstuvw +1234567890abcdefghijklmnopqrst +123 +1234567890abcdef +1234567890abcdefghijklm +1234567890abcdefghijklmn +1234567890abcdefghijk +1234567890abcdefgh +123456 +1234567 +1234567890a +1234567890abcdefghijklm +1 +1234567890abcdefghijklmnopq +1234567890abcdefghijkl +1234567890abcdefghijklmno +1234567890abcdefghijklmnopqrstuvwxyz +1234567890abcdefghijklmnopq +1234567890abcdefghijklmno +1234567890abcdefghijklmnopq +1234567890abcdefghijklmn +1234 +1234567890abcdefghijklmnop +123456789 +1234 +12345 +1234567890ab +1234567890abcdefghijklmnopqrstuvwxy +1234567890abcdefghijklmnopqrst +1234567890abcdefghijklmnopqrs +1234567890abcdefghijklmnopqrs +12345678 +1234567890ab +1234567890a +1234567890abcdefghijklmnopqr +123456 +1234567890abcdefghijkl +1234567890abcdefghijklm +1234567890abcdefghijklmnopqrstuvwxy +1234567890abcdefghijklmnopqrstuvwxy +1234567890abcdefghijklmnopqrstuv +1234567890abcdefghijklmnopqrstuv +1234567890abcdefghijklmnopqrstuvwx +1234567890abcde +1 +1234567890abcdefgh +1234567890abcdef +1234567890abcdefghi +1234567890 +1 +12345 +123456789 +1234567 +1234567890abcdefghi +1234567890abcdefghijklmnopqrstu +1234567890abcdefghijklmnopqrstuvwx +1234567890abcdefghijklmnopqrstuv +1234567890abcd +12345 +1234567890abcdefg +1234567890abcd +1234567890abcdefghijklmnopqrstuvw +1234567890abcdefghijklmnopqrstuvwxyz +12345678 +1234567890abcd +1234567890abcdefghijklmnop +123 +1234567890abcdefg +123456 +1234567890abcdefghij +1234567890abcdefg +1234567890abcdefghi +1234567890 +1234567890abcdefghijklmno +1234567890abcdefghij +1234567890abcdefghijklmn +1234567890abcde +1234567890abc +123456789 +1234567890abcdefgh +1234567890abcdefghij +1234567890abcdefghijklmnopqrstuvwx +1234567890abcdefghijklmnopqr +1234567890abcdefghijklmnopqrst +1234567890abcdefghijklmnopqrs +1234567890abcdefghijklmnopqrstuvwxyz +1234567890abcdefghijkl +1234 +12 +12 +12345678 +1234567 +1234567890 +123 +1234567890abc +1234567890abcdefghijklmnopqr +12 +1234567890abcde +1234567890abcdef +1234567890a +1234567890ab +1234567890abcdefghijk +1234567890abcdefghijklmnopqrstu +1234567890abcdefghijklmnop +1234567890abcdefghijk +1234567890abcdefghijklmnopqrstu +1234567890abcdefghijklmnopqrstuvw +1234567890abc diff --git a/lib/x1000-installer/test/data/lines_sorted.txt b/lib/x1000-installer/test/data/lines_sorted.txt new file mode 100644 index 0000000000..4944efd614 --- /dev/null +++ b/lib/x1000-installer/test/data/lines_sorted.txt @@ -0,0 +1,36 @@ +1 +12 +123 +1234 +12345 +123456 +1234567 +12345678 +123456789 +1234567890 +1234567890a +1234567890ab +1234567890abc +1234567890abcd +1234567890abcde +1234567890abcdef +1234567890abcdefg +1234567890abcdefgh +1234567890abcdefghi +1234567890abcdefghij +1234567890abcdefghijk +1234567890abcdefghijkl +1234567890abcdefghijklm +1234567890abcdefghijklmn +1234567890abcdefghijklmno +1234567890abcdefghijklmnop +1234567890abcdefghijklmnopq +1234567890abcdefghijklmnopqr +1234567890abcdefghijklmnopqrs +1234567890abcdefghijklmnopqrst +1234567890abcdefghijklmnopqrstu +1234567890abcdefghijklmnopqrstuv +1234567890abcdefghijklmnopqrstuvw +1234567890abcdefghijklmnopqrstuvwx +1234567890abcdefghijklmnopqrstuvwxy +1234567890abcdefghijklmnopqrstuvwxyz diff --git a/lib/x1000-installer/test/main.c b/lib/x1000-installer/test/main.c index 4d16271c98..9964e22716 100644 --- a/lib/x1000-installer/test/main.c +++ b/lib/x1000-installer/test/main.c @@ -39,8 +39,13 @@ struct test_info { test_t func; }; +extern void test_stream_read_lines(void); +extern void test_stream_read_line_too_long(void); + #define TEST(x) {#x, x} static const struct test_info all_tests[] = { + TEST(test_stream_read_lines), + TEST(test_stream_read_line_too_long), }; #undef TEST diff --git a/lib/x1000-installer/test/test_stream.c b/lib/x1000-installer/test/test_stream.c new file mode 100644 index 0000000000..b4a5723b76 --- /dev/null +++ b/lib/x1000-installer/test/test_stream.c @@ -0,0 +1,108 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2021 Aidan MacDonald + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#include "test.h" +#include "xf_stream.h" +#include "xf_error.h" +#include "file.h" +#include +#include +#include + +#define TESTDATADIR "test/data/" +#define TESTFILE_SHUFFLED TESTDATADIR "lines_shuffled.txt" +#define TESTFILE_SORTED TESTDATADIR "lines_sorted.txt" + +const char line_string[] = "1234567890abcdefghijklmnopqrstuvwxyz"; + +static int read_line_cb(int n, char* buf, void* arg) +{ + int* max_n = (int*)arg; + *max_n = n; + + T_ASSERT(strlen(buf) > 0); + T_ASSERT(strncmp(line_string, buf, strlen(buf)) == 0); + + cleanup: + return 0; +} + +void test_stream_read_lines(void) +{ + struct xf_stream s; + int rc; + char buf[100]; + bool s_open = false; + + for(int bufsz = 38; bufsz <= 100; bufsz += 1) { + rc = xf_open_file(TESTFILE_SHUFFLED, O_RDONLY, &s); + T_ASSERT(rc == 0); + s_open = true; + + int max_n = 0; + rc = xf_stream_read_lines(&s, buf, bufsz, &read_line_cb, &max_n); + T_ASSERT(rc == 0); + T_ASSERT(max_n+1 == 108); + + xf_stream_close(&s); + s_open = false; + } + + cleanup: + if(s_open) + xf_stream_close(&s); + return; +} + +void test_stream_read_line_too_long(void) +{ + struct xf_stream s; + int rc; + char buf[38]; + bool s_open = false; + + for(int bufsz = 0; bufsz <= 38; bufsz += 1) { + rc = xf_open_file(TESTFILE_SORTED, O_RDONLY, &s); + T_ASSERT(rc == 0); + s_open = true; + + int max_n = -1; + rc = xf_stream_read_lines(&s, buf, bufsz, &read_line_cb, &max_n); + if(bufsz == 38) { + T_ASSERT(rc == 0); + T_ASSERT(max_n+1 == 36); + } else { + T_ASSERT(rc == XF_E_LINE_TOO_LONG); + if(bufsz <= 1) + T_ASSERT(max_n == -1); + else + T_ASSERT(max_n+1 == bufsz-2); + } + + xf_stream_close(&s); + s_open = false; + } + + cleanup: + if(s_open) + xf_stream_close(&s); + return; +} -- cgit v1.2.3