x1000-installer: tests for xf_stream.c

Change-Id: Ib9d1ca0109029f09ff0f8adfc10925c7c9a0f8b9
This commit is contained in:
Aidan MacDonald 2021-11-28 13:44:46 +00:00
parent b027063c03
commit 420d545018
5 changed files with 259 additions and 1 deletions

View file

@ -12,7 +12,8 @@ TOBJ = test_lib/core_alloc.o \
test_lib/pathfuncs.o \ test_lib/pathfuncs.o \
test_lib/md5.o \ test_lib/md5.o \
test_lib/strlcpy.o \ test_lib/strlcpy.o \
test/main.o test/main.o \
test/test_stream.o
TBIN = xf_test TBIN = xf_test
# dependency needs to be built manually # dependency needs to be built manually

View file

@ -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

View file

@ -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

View file

@ -39,8 +39,13 @@ struct test_info {
test_t func; test_t func;
}; };
extern void test_stream_read_lines(void);
extern void test_stream_read_line_too_long(void);
#define TEST(x) {#x, x} #define TEST(x) {#x, x}
static const struct test_info all_tests[] = { static const struct test_info all_tests[] = {
TEST(test_stream_read_lines),
TEST(test_stream_read_line_too_long),
}; };
#undef TEST #undef TEST

View file

@ -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 <string.h>
#include <stdbool.h>
#include <stdio.h>
#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;
}