Fri, 01 Nov 2024 18:16:32 +0100
simplify parsing of array and object elements
relates to #431
782
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
4 | * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | * |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | * |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | * |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | */ |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
29 | #include "cx/test.h" |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | |
963
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
31 | #if __GNUC__ |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
32 | // we want to perform overflow tests, but we don't want warnings about them |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
33 | #define __alloc_size__(...) // NOLINT(*-reserved-identifier) |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
34 | #endif |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
35 | |
782
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
36 | #include "cx/allocator.h" |
963
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
37 | #include <errno.h> |
782
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
38 | |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
39 | CX_TEST(test_allocator_default) { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | CX_TEST_DO { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | CX_TEST_ASSERT(cxDefaultAllocator->cl != NULL); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | CX_TEST(test_allocator_default_malloc) { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | void *test = cxMalloc(cxDefaultAllocator, 16); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | CX_TEST_DO { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | CX_TEST_ASSERT(test != NULL); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | // we cannot assert sth. but valgrind will detect an error |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | memcpy(test, "0123456789ABCDEF", 16); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | free(test); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
54 | |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | CX_TEST(test_allocator_default_calloc) { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | char *test = cxCalloc(cxDefaultAllocator, 8, 2); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
57 | CX_TEST_DO { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | CX_TEST_ASSERT(test != NULL); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | for (int i = 0; i < 16; i++) { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | CX_TEST_ASSERT(test[i] == 0); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
61 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | free(test); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | CX_TEST(test_allocator_default_realloc) { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | char *test = calloc(8, 1); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | memcpy(test, "Test", 5); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | CX_TEST_DO { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | test = cxRealloc(cxDefaultAllocator, test, 16); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | CX_TEST_ASSERT(test != NULL); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | free(test); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
76 | |
963
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
77 | CX_TEST(test_allocator_default_reallocarray) { |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
78 | char *test = calloc(8, 1); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
79 | memcpy(test, "Test", 5); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
80 | CX_TEST_DO { |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
81 | test = cxReallocArray(cxDefaultAllocator, test, 16, 2); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
82 | CX_TEST_ASSERT(test != NULL); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
83 | CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
84 | } |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
85 | free(test); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
86 | } |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
87 | |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
88 | CX_TEST(test_allocator_default_reallocarray_overflow) { |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
89 | char *test = calloc(8, 1); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
90 | memcpy(test, "Test", 5); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
91 | CX_TEST_DO { |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
92 | void *fail = cxReallocArray(cxDefaultAllocator, test, SIZE_MAX/2, 4); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
93 | CX_TEST_ASSERT(errno == ENOMEM); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
94 | CX_TEST_ASSERT(fail == NULL); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
95 | CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
96 | } |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
97 | free(test); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
98 | } |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
99 | |
782
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
100 | CX_TEST(test_allocator_default_free) { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
101 | void *test = malloc(16); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
102 | CX_TEST_DO { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
103 | // we cannot assert sth. but valgrind will detect an error |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
104 | cxFree(cxDefaultAllocator, test); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
105 | CX_TEST_ASSERT(true); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
107 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
108 | |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
109 | CX_TEST(test_allocator_reallocate) { |
963
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
110 | char *test = calloc(8, 1); |
782
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
111 | memcpy(test, "Test", 5); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | CX_TEST_DO { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
113 | int ret = cxReallocate(cxDefaultAllocator, &test, 16); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
114 | CX_TEST_ASSERT(ret == 0); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
115 | CX_TEST_ASSERT(test != NULL); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
116 | CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
117 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
118 | free(test); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
119 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
120 | |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
121 | CX_TEST(test_allocator_reallocate_low_level) { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
122 | void *test = calloc(8, 1); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
123 | memcpy(test, "Test", 5); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
124 | CX_TEST_DO { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
125 | int ret = cx_reallocate(&test, 16); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
126 | CX_TEST_ASSERT(ret == 0); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
127 | CX_TEST_ASSERT(test != NULL); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
128 | CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
129 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
130 | free(test); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
131 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
132 | |
963
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
133 | CX_TEST(test_allocator_reallocatearray) { |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
134 | char *test = calloc(8, 1); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
135 | memcpy(test, "Test", 5); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
136 | CX_TEST_DO { |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
137 | int ret = cxReallocateArray(cxDefaultAllocator, &test, 16, 2); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
138 | CX_TEST_ASSERT(ret == 0); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
139 | CX_TEST_ASSERT(test != NULL); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
140 | CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
141 | } |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
142 | free(test); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
143 | } |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
144 | |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
145 | CX_TEST(test_allocator_reallocatearray_overflow) { |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
146 | char *test = calloc(8, 1); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
147 | memcpy(test, "Test", 5); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
148 | CX_TEST_DO { |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
149 | int ret = cxReallocateArray(cxDefaultAllocator, &test, SIZE_MAX/2, 4); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
150 | CX_TEST_ASSERT(ret != 0); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
151 | CX_TEST_ASSERT(errno == ENOMEM); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
152 | CX_TEST_ASSERT(test != NULL); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
153 | CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
154 | } |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
155 | free(test); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
156 | } |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
157 | |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
158 | CX_TEST(test_allocator_reallocatearray_low_level) { |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
159 | char *test = calloc(8, 1); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
160 | memcpy(test, "Test", 5); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
161 | CX_TEST_DO { |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
162 | int ret = cx_reallocatearray(&test, 16, 2); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
163 | CX_TEST_ASSERT(ret == 0); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
164 | CX_TEST_ASSERT(test != NULL); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
165 | CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
166 | } |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
167 | free(test); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
168 | } |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
169 | |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
170 | CX_TEST(test_allocator_reallocatearray_low_level_overflow) { |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
171 | char *test = calloc(8, 1); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
172 | memcpy(test, "Test", 5); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
173 | CX_TEST_DO { |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
174 | int ret = cx_reallocatearray(&test, SIZE_MAX/2, 4); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
175 | CX_TEST_ASSERT(ret != 0); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
176 | CX_TEST_ASSERT(errno == ENOMEM); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
177 | CX_TEST_ASSERT(test != NULL); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
178 | CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
179 | } |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
180 | free(test); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
181 | } |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
182 | |
782
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
183 | static void *test_allocator_mock_failing_realloc( |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
184 | __attribute__((__unused__))void *p, |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
185 | __attribute__((__unused__))void *d, |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
186 | __attribute__((__unused__))size_t n |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
187 | ) { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
188 | return NULL; |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
189 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
190 | |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
191 | CX_TEST(test_allocator_reallocate_fails) { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
192 | // Mock an allocator that always returns NULL on realloc |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
193 | cx_allocator_class mock_cl; |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
194 | mock_cl.realloc = test_allocator_mock_failing_realloc; |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
195 | CxAllocator mock = {&mock_cl, NULL}; |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
196 | |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
197 | void *test = calloc(8, 1); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
198 | memcpy(test, "Test", 5); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
199 | void *original = test; |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
200 | CX_TEST_DO { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
201 | int ret = cxReallocate(&mock, &test, 16); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
202 | // non-zero return code because of the failure |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
203 | CX_TEST_ASSERT(ret != 0); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
204 | // the test pointer was not changed and still points to the same memory |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
205 | CX_TEST_ASSERT(test == original); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
206 | CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
207 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
208 | free(test); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
209 | } |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
210 | |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
211 | CxTestSuite *cx_test_suite_allocator(void) { |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
212 | CxTestSuite *suite = cx_test_suite_new("allocator"); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
213 | |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
214 | cx_test_register(suite, test_allocator_default); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
215 | cx_test_register(suite, test_allocator_default_malloc); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
216 | cx_test_register(suite, test_allocator_default_calloc); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
217 | cx_test_register(suite, test_allocator_default_realloc); |
963
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
218 | cx_test_register(suite, test_allocator_default_reallocarray); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
219 | cx_test_register(suite, test_allocator_default_reallocarray_overflow); |
782
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
220 | cx_test_register(suite, test_allocator_default_free); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
221 | cx_test_register(suite, test_allocator_reallocate); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
222 | cx_test_register(suite, test_allocator_reallocate_fails); |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
223 | cx_test_register(suite, test_allocator_reallocate_low_level); |
963
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
224 | cx_test_register(suite, test_allocator_reallocatearray); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
225 | cx_test_register(suite, test_allocator_reallocatearray_overflow); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
226 | cx_test_register(suite, test_allocator_reallocatearray_low_level); |
2f601274bbac
add reallocarray() like functions - resolves #468
Mike Becker <universe@uap-core.de>
parents:
782
diff
changeset
|
227 | cx_test_register(suite, test_allocator_reallocatearray_low_level_overflow); |
782
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
228 | |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
229 | return suite; |
74d777455e96
migrate allocator tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
230 | } |