-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathchainbuffer.c
105 lines (84 loc) · 3.03 KB
/
chainbuffer.c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* vrms-rpm - list non-free packages on an rpm-based Linux distribution
* Copyright (C) 2021, 2023 suve (a.k.a. Artur Frenszek-Iwicki)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program 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
* this program (LICENCE.txt). If not, see <http://www.gnu.org/licenses/>.
*/
// The arg/def/jmp includes are required by cmocka.
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <time.h>
#include "src/buffers.h"
#define UNUSED(x) ((void)(x))
#define CHAINBUF_CAPACITY 2000
int test_setup__chainbuffer(void **state) {
srand(time(NULL));
struct ChainBuffer *cb = chainbuf_init(CHAINBUF_CAPACITY);
assert_non_null(cb);
*state = cb;
return 0;
}
int test_teardown__chainbuffer(void **state) {
struct ChainBuffer *cb = *state;
chainbuf_free(cb);
return 0;
}
static int random(const int min, const int max) {
return min + (rand() % (max - min + 1));
}
static void randomize_string(char *buffer, const size_t bufsize) {
for(size_t i = 0; i < bufsize - 1; ++i) {
buffer[i] = random(0x21, 0x7e);
}
buffer[bufsize - 1] = '\0';
}
void test__chainbuffer(void **state) {
struct ChainBuffer **cb = (void*)state;
const char *first_data = "some text appended at start";
char *first_append = chainbuf_append(cb, first_data);
assert_string_equal(first_append, first_data);
// Lots of small appends.
for(int i = 0; i < 5000; ++i) {
const char *short_text = "abc";
char *append = chainbuf_append(cb, short_text);
assert_string_equal(append, short_text);
}
const char *second_data = "some other text appended in the middle";
char *second_append = chainbuf_append(cb, second_data);
assert_string_equal(second_append, second_data);
// A few max size appends.
for(int i = 0; i < 4; ++i) {
char buffer[CHAINBUF_CAPACITY];
randomize_string(buffer, sizeof(buffer));
char *append = chainbuf_append(cb, buffer);
assert_memory_equal(append, buffer, sizeof(buffer));
}
const char *third_data = "yet another text somewhere in the middle";
char *third_append = chainbuf_append(cb, third_data);
assert_string_equal(third_append, third_data);
// Couple of longer appends (anywhere from 128 to 1024 chars).
for(int i = 0; i < 20; ++i) {
char buffer[1024];
const size_t size = random(128, sizeof(buffer));
randomize_string(buffer, size);
char *append = chainbuf_append(cb, buffer);
assert_memory_equal(append, buffer, size);
}
// Check if nothing bad happened to our earlier appends.
assert_string_equal(first_append, first_data);
assert_string_equal(second_append, second_data);
assert_string_equal(third_append, third_data);
}