summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAidan MacDonald <amachronic@protonmail.com>2021-11-28 13:44:46 +0000
committerAidan MacDonald <amachronic@protonmail.com>2021-11-28 13:55:43 +0000
commit420d545018dbb21956b4c441b42c7e4803d95a1d (patch)
tree3f0e3f598de478905d2d532bedc8437ea0911f82
parentb027063c030cc0cd034b6ec3aa6bc4885b019c88 (diff)
downloadrockbox-420d545018dbb21956b4c441b42c7e4803d95a1d.tar.gz
rockbox-420d545018dbb21956b4c441b42c7e4803d95a1d.zip
x1000-installer: tests for xf_stream.c
Change-Id: Ib9d1ca0109029f09ff0f8adfc10925c7c9a0f8b9
-rw-r--r--lib/x1000-installer/Makefile3
-rw-r--r--lib/x1000-installer/test/data/lines_shuffled.txt108
-rw-r--r--lib/x1000-installer/test/data/lines_sorted.txt36
-rw-r--r--lib/x1000-installer/test/main.c5
-rw-r--r--lib/x1000-installer/test/test_stream.c108
5 files changed, 259 insertions, 1 deletions
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 \
12 test_lib/pathfuncs.o \ 12 test_lib/pathfuncs.o \
13 test_lib/md5.o \ 13 test_lib/md5.o \
14 test_lib/strlcpy.o \ 14 test_lib/strlcpy.o \
15 test/main.o 15 test/main.o \
16 test/test_stream.o
16TBIN = xf_test 17TBIN = xf_test
17 18
18# dependency needs to be built manually 19# 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 @@
11234567890abcdefghijklmnopqrstuvw
21234567890abcdefghijklmnopqrst
3123
41234567890abcdef
51234567890abcdefghijklm
61234567890abcdefghijklmn
71234567890abcdefghijk
81234567890abcdefgh
9123456
101234567
111234567890a
121234567890abcdefghijklm
131
141234567890abcdefghijklmnopq
151234567890abcdefghijkl
161234567890abcdefghijklmno
171234567890abcdefghijklmnopqrstuvwxyz
181234567890abcdefghijklmnopq
191234567890abcdefghijklmno
201234567890abcdefghijklmnopq
211234567890abcdefghijklmn
221234
231234567890abcdefghijklmnop
24123456789
251234
2612345
271234567890ab
281234567890abcdefghijklmnopqrstuvwxy
291234567890abcdefghijklmnopqrst
301234567890abcdefghijklmnopqrs
311234567890abcdefghijklmnopqrs
3212345678
331234567890ab
341234567890a
351234567890abcdefghijklmnopqr
36123456
371234567890abcdefghijkl
381234567890abcdefghijklm
391234567890abcdefghijklmnopqrstuvwxy
401234567890abcdefghijklmnopqrstuvwxy
411234567890abcdefghijklmnopqrstuv
421234567890abcdefghijklmnopqrstuv
431234567890abcdefghijklmnopqrstuvwx
441234567890abcde
451
461234567890abcdefgh
471234567890abcdef
481234567890abcdefghi
491234567890
501
5112345
52123456789
531234567
541234567890abcdefghi
551234567890abcdefghijklmnopqrstu
561234567890abcdefghijklmnopqrstuvwx
571234567890abcdefghijklmnopqrstuv
581234567890abcd
5912345
601234567890abcdefg
611234567890abcd
621234567890abcdefghijklmnopqrstuvw
631234567890abcdefghijklmnopqrstuvwxyz
6412345678
651234567890abcd
661234567890abcdefghijklmnop
67123
681234567890abcdefg
69123456
701234567890abcdefghij
711234567890abcdefg
721234567890abcdefghi
731234567890
741234567890abcdefghijklmno
751234567890abcdefghij
761234567890abcdefghijklmn
771234567890abcde
781234567890abc
79123456789
801234567890abcdefgh
811234567890abcdefghij
821234567890abcdefghijklmnopqrstuvwx
831234567890abcdefghijklmnopqr
841234567890abcdefghijklmnopqrst
851234567890abcdefghijklmnopqrs
861234567890abcdefghijklmnopqrstuvwxyz
871234567890abcdefghijkl
881234
8912
9012
9112345678
921234567
931234567890
94123
951234567890abc
961234567890abcdefghijklmnopqr
9712
981234567890abcde
991234567890abcdef
1001234567890a
1011234567890ab
1021234567890abcdefghijk
1031234567890abcdefghijklmnopqrstu
1041234567890abcdefghijklmnop
1051234567890abcdefghijk
1061234567890abcdefghijklmnopqrstu
1071234567890abcdefghijklmnopqrstuvw
1081234567890abc
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 @@
11
212
3123
41234
512345
6123456
71234567
812345678
9123456789
101234567890
111234567890a
121234567890ab
131234567890abc
141234567890abcd
151234567890abcde
161234567890abcdef
171234567890abcdefg
181234567890abcdefgh
191234567890abcdefghi
201234567890abcdefghij
211234567890abcdefghijk
221234567890abcdefghijkl
231234567890abcdefghijklm
241234567890abcdefghijklmn
251234567890abcdefghijklmno
261234567890abcdefghijklmnop
271234567890abcdefghijklmnopq
281234567890abcdefghijklmnopqr
291234567890abcdefghijklmnopqrs
301234567890abcdefghijklmnopqrst
311234567890abcdefghijklmnopqrstu
321234567890abcdefghijklmnopqrstuv
331234567890abcdefghijklmnopqrstuvw
341234567890abcdefghijklmnopqrstuvwx
351234567890abcdefghijklmnopqrstuvwxy
361234567890abcdefghijklmnopqrstuvwxyz
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 {
39 test_t func; 39 test_t func;
40}; 40};
41 41
42extern void test_stream_read_lines(void);
43extern void test_stream_read_line_too_long(void);
44
42#define TEST(x) {#x, x} 45#define TEST(x) {#x, x}
43static const struct test_info all_tests[] = { 46static const struct test_info all_tests[] = {
47 TEST(test_stream_read_lines),
48 TEST(test_stream_read_line_too_long),
44}; 49};
45#undef TEST 50#undef TEST
46 51
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 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2021 Aidan MacDonald
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "test.h"
23#include "xf_stream.h"
24#include "xf_error.h"
25#include "file.h"
26#include <string.h>
27#include <stdbool.h>
28#include <stdio.h>
29
30#define TESTDATADIR "test/data/"
31#define TESTFILE_SHUFFLED TESTDATADIR "lines_shuffled.txt"
32#define TESTFILE_SORTED TESTDATADIR "lines_sorted.txt"
33
34const char line_string[] = "1234567890abcdefghijklmnopqrstuvwxyz";
35
36static int read_line_cb(int n, char* buf, void* arg)
37{
38 int* max_n = (int*)arg;
39 *max_n = n;
40
41 T_ASSERT(strlen(buf) > 0);
42 T_ASSERT(strncmp(line_string, buf, strlen(buf)) == 0);
43
44 cleanup:
45 return 0;
46}
47
48void test_stream_read_lines(void)
49{
50 struct xf_stream s;
51 int rc;
52 char buf[100];
53 bool s_open = false;
54
55 for(int bufsz = 38; bufsz <= 100; bufsz += 1) {
56 rc = xf_open_file(TESTFILE_SHUFFLED, O_RDONLY, &s);
57 T_ASSERT(rc == 0);
58 s_open = true;
59
60 int max_n = 0;
61 rc = xf_stream_read_lines(&s, buf, bufsz, &read_line_cb, &max_n);
62 T_ASSERT(rc == 0);
63 T_ASSERT(max_n+1 == 108);
64
65 xf_stream_close(&s);
66 s_open = false;
67 }
68
69 cleanup:
70 if(s_open)
71 xf_stream_close(&s);
72 return;
73}
74
75void test_stream_read_line_too_long(void)
76{
77 struct xf_stream s;
78 int rc;
79 char buf[38];
80 bool s_open = false;
81
82 for(int bufsz = 0; bufsz <= 38; bufsz += 1) {
83 rc = xf_open_file(TESTFILE_SORTED, O_RDONLY, &s);
84 T_ASSERT(rc == 0);
85 s_open = true;
86
87 int max_n = -1;
88 rc = xf_stream_read_lines(&s, buf, bufsz, &read_line_cb, &max_n);
89 if(bufsz == 38) {
90 T_ASSERT(rc == 0);
91 T_ASSERT(max_n+1 == 36);
92 } else {
93 T_ASSERT(rc == XF_E_LINE_TOO_LONG);
94 if(bufsz <= 1)
95 T_ASSERT(max_n == -1);
96 else
97 T_ASSERT(max_n+1 == bufsz-2);
98 }
99
100 xf_stream_close(&s);
101 s_open = false;
102 }
103
104 cleanup:
105 if(s_open)
106 xf_stream_close(&s);
107 return;
108}