summaryrefslogtreecommitdiff
path: root/test/test_io.c
blob: 86dde8ea3143318102787679c54ede99041955d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* test_io.c -- MinUnit test cases for io.c

   Copyright (C) 2017 Jakob Tsar-Fox, All Rights Reserved.

   This file is part of Nekopack.

   Nekopack 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 3 of the License, or (at
   your option) any later version.

   Nekopack is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Nekopack. If not, see <http://www.gnu.org/licenses/>. */

#include <stdint.h>
#include <string.h>

#include "minunit.h"

#include "io.h"

extern int tests_run;


char *test_stream_obj(void) {
    struct stream *s_1 = stream_new(0x10);
    struct stream *s_2 = stream_new(0x20);
    struct stream *s_3 = stream_new(0x40);
    mu_assert("Incorrect size for `s_1`", s_1->len == 0x10);
    mu_assert("Incorrect size for `s_2`", s_2->len == 0x20);
    mu_assert("Incorrect size for `s_3`", s_3->len == 0x40);
    stream_free(s_1);
    stream_free(s_2);
    stream_free(s_3);
    return NULL;
}


char *test_stream_rw(void) {
    char dest[4];
    struct stream *s = stream_new(0x4);
    stream_write(s, "\x00\x01\x02\x03", 4);
    mu_assert("Write failure", !memcmp(s->_start, "\x00\x01\x02\x03", 4));
    mu_assert("Cursor not incremented", s->_cur - s->_start == 4);
    s->_cur = s->_start;

    stream_read(dest, s, 4);
    mu_assert("Read failure", !memcmp(dest, "\x00\x01\x02\x03", 4));
    mu_assert("Cursor not incremented", s->_cur - s->_start == 4);
    stream_free(s);
    return NULL;
}


char *test_stream_realloc(void) {
    struct stream *s = stream_new(0x2);
    stream_write(s, "\x00\x01\x02\x03", 4);
    mu_assert("`len` not modified", s->len > 0x2);
    return NULL;
}


char *test_stream_xor(void) {
    struct stream *s = stream_new(2);
    stream_write(s, "\x01\x01", 2);
    stream_xor(s, 1, 0);
    mu_assert("Initial key failure", !memcmp(s->_start, "\x00\x01", 2));
    stream_xor(s, 0, 1);
    mu_assert("Primary key failure", !memcmp(s->_start, "\x01\x00", 2));
    stream_free(s);
    return NULL;
}


char *test_stream_nav(void) {
    struct stream *s = stream_new(2);
    mu_assert("Cursor not at beginning", stream_tell(s) == 0);
    stream_seek(s, 2);
    mu_assert("Cursor not advanced", stream_tell(s) == 2);
    stream_rewind(s);
    mu_assert("Cursor not rewinded", stream_tell(s) == 0);
    stream_free(s);
    return NULL;
}