]>
Commit | Line | Data |
---|---|---|
f5e7e959 | 1 | /* Tests for <support/blob_repeat.h> |
04277e02 | 2 | Copyright (C) 2018-2019 Free Software Foundation, Inc. |
f5e7e959 FW |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
f5e7e959 FW |
18 | |
19 | #include <stdio.h> | |
20 | #include <support/blob_repeat.h> | |
21 | #include <support/check.h> | |
22 | ||
23 | static int | |
24 | do_test (void) | |
25 | { | |
26 | struct support_blob_repeat repeat | |
27 | = support_blob_repeat_allocate ("5", 1, 5); | |
28 | TEST_COMPARE_BLOB (repeat.start, repeat.size, "55555", 5); | |
29 | support_blob_repeat_free (&repeat); | |
30 | ||
31 | repeat = support_blob_repeat_allocate ("ABC", 3, 3); | |
32 | TEST_COMPARE_BLOB (repeat.start, repeat.size, "ABCABCABC", 9); | |
33 | support_blob_repeat_free (&repeat); | |
34 | ||
35 | repeat = support_blob_repeat_allocate ("abc", 4, 3); | |
36 | TEST_COMPARE_BLOB (repeat.start, repeat.size, "abc\0abc\0abc", 12); | |
37 | support_blob_repeat_free (&repeat); | |
38 | ||
39 | size_t gigabyte = 1U << 30; | |
40 | repeat = support_blob_repeat_allocate ("X", 1, gigabyte + 1); | |
41 | if (repeat.start == NULL) | |
42 | puts ("warning: not enough memory for 1 GiB mapping"); | |
43 | else | |
44 | { | |
45 | TEST_COMPARE (repeat.size, gigabyte + 1); | |
46 | { | |
47 | unsigned char *p = repeat.start; | |
48 | for (size_t i = 0; i < gigabyte + 1; ++i) | |
49 | if (p[i] != 'X') | |
50 | FAIL_EXIT1 ("invalid byte 0x%02x at %zu", p[i], i); | |
51 | ||
52 | /* Check that there is no sharing across the mapping. */ | |
53 | p[0] = 'Y'; | |
54 | p[1U << 24] = 'Z'; | |
55 | for (size_t i = 0; i < gigabyte + 1; ++i) | |
56 | if (i == 0) | |
57 | TEST_COMPARE (p[i], 'Y'); | |
58 | else if (i == 1U << 24) | |
59 | TEST_COMPARE (p[i], 'Z'); | |
60 | else if (p[i] != 'X') | |
61 | FAIL_EXIT1 ("invalid byte 0x%02x at %zu", p[i], i); | |
62 | } | |
63 | } | |
64 | support_blob_repeat_free (&repeat); | |
65 | ||
66 | repeat = support_blob_repeat_allocate ("012345678", 9, 10 * 1000 * 1000); | |
67 | if (repeat.start == NULL) | |
68 | puts ("warning: not enough memory for large mapping"); | |
69 | else | |
70 | { | |
71 | unsigned char *p = repeat.start; | |
72 | for (int i = 0; i < 10 * 1000 * 1000; ++i) | |
73 | for (int j = 0; j <= 8; ++j) | |
74 | if (p[i * 9 + j] != '0' + j) | |
75 | { | |
76 | printf ("error: element %d index %d\n", i, j); | |
77 | TEST_COMPARE (p[i * 9 + j], '0' + j); | |
78 | } | |
79 | } | |
80 | support_blob_repeat_free (&repeat); | |
81 | ||
82 | return 0; | |
83 | } | |
84 | ||
85 | #include <support/test-driver.c> |