/
source_t0.c
115 lines (104 loc) · 3.15 KB
/
source_t0.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
106
107
108
109
110
111
112
113
114
115
/* Part of the culibs project, <http://www.eideticdew.org/culibs/>.
* Copyright (C) 2010 Petter Urkedal <paurkedal@eideticdew.org>
*
* 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 3 of the License, or
* (at your option) any later version.
*
* 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. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cutext/source.h>
#include <cu/test.h>
#include <cu/memory.h>
#include <cu/size.h>
#include <cu/wstring.h>
static cutext_source_t
_make_cstr_source(size_t n)
{
int i;
char *buf = cu_galloc(n + 1);
for (i = 0; i < n; ++i)
buf[i] = 'a' + i % 26;
buf[i] = 0;
return cutext_source_new_cstr(buf);
}
static cutext_source_t
_make_wstr_source(size_t n)
{
int i;
cu_wchar_t *buf = cu_galloc((n + 1) * sizeof(cu_wchar_t));
cu_wstring_t wstr;
for (i = 0; i < n; ++i)
buf[i] = 'a' + i % 26;
buf[i] = 0;
wstr = cu_wstring_of_arr(buf, n);
return cutext_source_new_wstring(wstr);
}
static cutext_source_t
_make_wraw_source(size_t n)
{
int i;
cu_wchar_t *buf = cu_galloc((n + 1) * sizeof(cu_wchar_t));
for (i = 0; i < n; ++i)
buf[i] = 'a' + i % 26;
buf[i] = 0;
return cutext_source_new_mem(NULL, buf, n*sizeof(cu_wchar_t));
}
static void
_verify_read(cutext_source_t src, size_t n)
{
int i = 0;
char *buf = cu_galloc(n);
while (i < n) {
size_t j, m = lrand48() % n + 1;
size_t k = cu_size_min(n - i, m);
cu_test_assert(cutext_source_read(src, buf, m) == k);
for (j = 0; j < k; ++j, ++i)
cu_test_assert(buf[j] == 'a' + i % 26);
}
cu_test_assert(cutext_source_read(src, buf, 1) == 0);
}
static void
_verify_look(cutext_source_t src, size_t n)
{
int i = 0;
while (i < n) {
size_t j, m = lrand48() % n + 1;
size_t k;
char const *buf = cutext_source_look(src, m, &k);
cu_test_assert(k == cu_size_min(n - i, m));
for (j = 0; j < k; ++j, ++i)
cu_test_assert(buf[j] == 'a' + i % 26);
cu_test_assert(cutext_source_read(src, NULL, m) == k);
}
cu_test_assert(cutext_source_read(src, NULL, 1) == 0);
}
static void
_test_cstr_source(size_t n)
{
cutext_source_t src;
src = _make_wstr_source(n);
cu_test_assert_size_eq(cutext_source_count(src), n*sizeof(cu_wchar_t));
src = cutext_source_stack_iconv("utf-8", _make_wstr_source(n));
cu_test_assert_size_eq(cutext_source_count(src), n);
_verify_read(_make_cstr_source(n), n);
_verify_look(cutext_source_stack_buffer(_make_cstr_source(n)), n);
_verify_read(cutext_source_stack_iconv("utf-8", _make_wstr_source(n)), n);
_verify_read(cutext_source_stack_iconv("utf-8", _make_wraw_source(n)), n);
}
int
main()
{
int n;
cutext_init();
for (n = 1; n < 7000; n = n*3/2 + 1)
_test_cstr_source(n);
return 2*!!cu_test_bug_count();
}