diff options
author | Aidan MacDonald <amachronic@protonmail.com> | 2021-11-28 13:44:46 +0000 |
---|---|---|
committer | Aidan MacDonald <amachronic@protonmail.com> | 2021-11-28 13:55:43 +0000 |
commit | 420d545018dbb21956b4c441b42c7e4803d95a1d (patch) | |
tree | 3f0e3f598de478905d2d532bedc8437ea0911f82 | |
parent | b027063c030cc0cd034b6ec3aa6bc4885b019c88 (diff) | |
download | rockbox-420d545018dbb21956b4c441b42c7e4803d95a1d.tar.gz rockbox-420d545018dbb21956b4c441b42c7e4803d95a1d.zip |
x1000-installer: tests for xf_stream.c
Change-Id: Ib9d1ca0109029f09ff0f8adfc10925c7c9a0f8b9
-rw-r--r-- | lib/x1000-installer/Makefile | 3 | ||||
-rw-r--r-- | lib/x1000-installer/test/data/lines_shuffled.txt | 108 | ||||
-rw-r--r-- | lib/x1000-installer/test/data/lines_sorted.txt | 36 | ||||
-rw-r--r-- | lib/x1000-installer/test/main.c | 5 | ||||
-rw-r--r-- | lib/x1000-installer/test/test_stream.c | 108 |
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 | ||
16 | TBIN = xf_test | 17 | TBIN = 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 @@ | |||
1 | 1234567890abcdefghijklmnopqrstuvw | ||
2 | 1234567890abcdefghijklmnopqrst | ||
3 | 123 | ||
4 | 1234567890abcdef | ||
5 | 1234567890abcdefghijklm | ||
6 | 1234567890abcdefghijklmn | ||
7 | 1234567890abcdefghijk | ||
8 | 1234567890abcdefgh | ||
9 | 123456 | ||
10 | 1234567 | ||
11 | 1234567890a | ||
12 | 1234567890abcdefghijklm | ||
13 | 1 | ||
14 | 1234567890abcdefghijklmnopq | ||
15 | 1234567890abcdefghijkl | ||
16 | 1234567890abcdefghijklmno | ||
17 | 1234567890abcdefghijklmnopqrstuvwxyz | ||
18 | 1234567890abcdefghijklmnopq | ||
19 | 1234567890abcdefghijklmno | ||
20 | 1234567890abcdefghijklmnopq | ||
21 | 1234567890abcdefghijklmn | ||
22 | 1234 | ||
23 | 1234567890abcdefghijklmnop | ||
24 | 123456789 | ||
25 | 1234 | ||
26 | 12345 | ||
27 | 1234567890ab | ||
28 | 1234567890abcdefghijklmnopqrstuvwxy | ||
29 | 1234567890abcdefghijklmnopqrst | ||
30 | 1234567890abcdefghijklmnopqrs | ||
31 | 1234567890abcdefghijklmnopqrs | ||
32 | 12345678 | ||
33 | 1234567890ab | ||
34 | 1234567890a | ||
35 | 1234567890abcdefghijklmnopqr | ||
36 | 123456 | ||
37 | 1234567890abcdefghijkl | ||
38 | 1234567890abcdefghijklm | ||
39 | 1234567890abcdefghijklmnopqrstuvwxy | ||
40 | 1234567890abcdefghijklmnopqrstuvwxy | ||
41 | 1234567890abcdefghijklmnopqrstuv | ||
42 | 1234567890abcdefghijklmnopqrstuv | ||
43 | 1234567890abcdefghijklmnopqrstuvwx | ||
44 | 1234567890abcde | ||
45 | 1 | ||
46 | 1234567890abcdefgh | ||
47 | 1234567890abcdef | ||
48 | 1234567890abcdefghi | ||
49 | 1234567890 | ||
50 | 1 | ||
51 | 12345 | ||
52 | 123456789 | ||
53 | 1234567 | ||
54 | 1234567890abcdefghi | ||
55 | 1234567890abcdefghijklmnopqrstu | ||
56 | 1234567890abcdefghijklmnopqrstuvwx | ||
57 | 1234567890abcdefghijklmnopqrstuv | ||
58 | 1234567890abcd | ||
59 | 12345 | ||
60 | 1234567890abcdefg | ||
61 | 1234567890abcd | ||
62 | 1234567890abcdefghijklmnopqrstuvw | ||
63 | 1234567890abcdefghijklmnopqrstuvwxyz | ||
64 | 12345678 | ||
65 | 1234567890abcd | ||
66 | 1234567890abcdefghijklmnop | ||
67 | 123 | ||
68 | 1234567890abcdefg | ||
69 | 123456 | ||
70 | 1234567890abcdefghij | ||
71 | 1234567890abcdefg | ||
72 | 1234567890abcdefghi | ||
73 | 1234567890 | ||
74 | 1234567890abcdefghijklmno | ||
75 | 1234567890abcdefghij | ||
76 | 1234567890abcdefghijklmn | ||
77 | 1234567890abcde | ||
78 | 1234567890abc | ||
79 | 123456789 | ||
80 | 1234567890abcdefgh | ||
81 | 1234567890abcdefghij | ||
82 | 1234567890abcdefghijklmnopqrstuvwx | ||
83 | 1234567890abcdefghijklmnopqr | ||
84 | 1234567890abcdefghijklmnopqrst | ||
85 | 1234567890abcdefghijklmnopqrs | ||
86 | 1234567890abcdefghijklmnopqrstuvwxyz | ||
87 | 1234567890abcdefghijkl | ||
88 | 1234 | ||
89 | 12 | ||
90 | 12 | ||
91 | 12345678 | ||
92 | 1234567 | ||
93 | 1234567890 | ||
94 | 123 | ||
95 | 1234567890abc | ||
96 | 1234567890abcdefghijklmnopqr | ||
97 | 12 | ||
98 | 1234567890abcde | ||
99 | 1234567890abcdef | ||
100 | 1234567890a | ||
101 | 1234567890ab | ||
102 | 1234567890abcdefghijk | ||
103 | 1234567890abcdefghijklmnopqrstu | ||
104 | 1234567890abcdefghijklmnop | ||
105 | 1234567890abcdefghijk | ||
106 | 1234567890abcdefghijklmnopqrstu | ||
107 | 1234567890abcdefghijklmnopqrstuvw | ||
108 | 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 | 1 | ||
2 | 12 | ||
3 | 123 | ||
4 | 1234 | ||
5 | 12345 | ||
6 | 123456 | ||
7 | 1234567 | ||
8 | 12345678 | ||
9 | 123456789 | ||
10 | 1234567890 | ||
11 | 1234567890a | ||
12 | 1234567890ab | ||
13 | 1234567890abc | ||
14 | 1234567890abcd | ||
15 | 1234567890abcde | ||
16 | 1234567890abcdef | ||
17 | 1234567890abcdefg | ||
18 | 1234567890abcdefgh | ||
19 | 1234567890abcdefghi | ||
20 | 1234567890abcdefghij | ||
21 | 1234567890abcdefghijk | ||
22 | 1234567890abcdefghijkl | ||
23 | 1234567890abcdefghijklm | ||
24 | 1234567890abcdefghijklmn | ||
25 | 1234567890abcdefghijklmno | ||
26 | 1234567890abcdefghijklmnop | ||
27 | 1234567890abcdefghijklmnopq | ||
28 | 1234567890abcdefghijklmnopqr | ||
29 | 1234567890abcdefghijklmnopqrs | ||
30 | 1234567890abcdefghijklmnopqrst | ||
31 | 1234567890abcdefghijklmnopqrstu | ||
32 | 1234567890abcdefghijklmnopqrstuv | ||
33 | 1234567890abcdefghijklmnopqrstuvw | ||
34 | 1234567890abcdefghijklmnopqrstuvwx | ||
35 | 1234567890abcdefghijklmnopqrstuvwxy | ||
36 | 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 { | |||
39 | test_t func; | 39 | test_t func; |
40 | }; | 40 | }; |
41 | 41 | ||
42 | extern void test_stream_read_lines(void); | ||
43 | extern void test_stream_read_line_too_long(void); | ||
44 | |||
42 | #define TEST(x) {#x, x} | 45 | #define TEST(x) {#x, x} |
43 | static const struct test_info all_tests[] = { | 46 | static 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 | |||
34 | const char line_string[] = "1234567890abcdefghijklmnopqrstuvwxyz"; | ||
35 | |||
36 | static 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 | |||
48 | void 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 | |||
75 | void 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 | } | ||