| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************/ | ||
| 2 | /* Copyright 2009-2026 Barcelona Supercomputing Center */ | ||
| 3 | /* */ | ||
| 4 | /* This file is part of the DLB library. */ | ||
| 5 | /* */ | ||
| 6 | /* DLB is free software: you can redistribute it and/or modify */ | ||
| 7 | /* it under the terms of the GNU Lesser General Public License as published by */ | ||
| 8 | /* the Free Software Foundation, either version 3 of the License, or */ | ||
| 9 | /* (at your option) any later version. */ | ||
| 10 | /* */ | ||
| 11 | /* DLB is distributed in the hope that it will be useful, */ | ||
| 12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | ||
| 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | ||
| 14 | /* GNU Lesser General Public License for more details. */ | ||
| 15 | /* */ | ||
| 16 | /* You should have received a copy of the GNU Lesser General Public License */ | ||
| 17 | /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */ | ||
| 18 | /*********************************************************************************/ | ||
| 19 | |||
| 20 | #include "support/small_array.h" | ||
| 21 | |||
| 22 | #include <stdio.h> | ||
| 23 | #include <assert.h> | ||
| 24 | |||
| 25 | /* Returns 1 if the pointer lives inside the struct's inline buffer. */ | ||
| 26 | #define IS_INLINE(name) ((name) == ((__##name).buffer)) | ||
| 27 | |||
| 28 | /* Returns the expected capacity for a type */ | ||
| 29 | #define EXPECTED_CAP(T) \ | ||
| 30 | ((SMALL_ARRAY_DEFAULT_BYTES >= sizeof(T)) ? SMALL_ARRAY_DEFAULT_BYTES / sizeof(T) : 1) | ||
| 31 | |||
| 32 | |||
| 33 | 1 | static void test_zero_size(void) | |
| 34 | { | ||
| 35 | 2 | SMALL_ARRAY(int, arr, 0); | |
| 36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(arr != NULL); |
| 37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(__arr.size == 0); |
| 38 | 1 | } | |
| 39 | |||
| 40 | 1 | static void test_size_one(void) | |
| 41 | { | ||
| 42 | 2 | SMALL_ARRAY(int, arr, 1); | |
| 43 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(IS_INLINE(arr)); |
| 44 | 1 | arr[0] = 42; | |
| 45 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(arr[0] == 42); |
| 46 | 1 | } | |
| 47 | |||
| 48 | 1 | static void test_int_inline(void) | |
| 49 | { | ||
| 50 | 1 | const size_t N = (SMALL_ARRAY_DEFAULT_BYTES / 2) / sizeof(int); | |
| 51 | 2 | SMALL_ARRAY(int, arr, N); | |
| 52 | |||
| 53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(IS_INLINE(arr) && "int: small array should use inline buffer"); |
| 54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(__arr.size == N); |
| 55 | |||
| 56 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 1 times.
|
129 | for (size_t i = 0; i < N; i++) arr[i] = (int)i; |
| 57 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
✓ Branch 3 taken 128 times.
✓ Branch 4 taken 1 times.
|
129 | for (size_t i = 0; i < N; i++) assert(arr[i] == (int)i); |
| 58 | 1 | } | |
| 59 | |||
| 60 | 1 | static void test_int_heap(void) | |
| 61 | { | ||
| 62 | 1 | const size_t N = (SMALL_ARRAY_DEFAULT_BYTES * 2) / sizeof(int); | |
| 63 | 2 | SMALL_ARRAY(int, arr, N); | |
| 64 | |||
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(!IS_INLINE(arr) && "int: large array should heap-allocate"); |
| 66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(__arr.size == N); |
| 67 | |||
| 68 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 1 times.
|
513 | for (size_t i = 0; i < N; i++) arr[i] = (int)i; |
| 69 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 512 times.
✓ Branch 3 taken 512 times.
✓ Branch 4 taken 1 times.
|
513 | for (size_t i = 0; i < N; i++) assert(arr[i] == (int)i); |
| 70 | 1 | } | |
| 71 | |||
| 72 | 1 | static void test_int_boundary(void) | |
| 73 | { | ||
| 74 | 1 | const size_t N = SMALL_ARRAY_DEFAULT_BYTES / sizeof(int); | |
| 75 | 2 | SMALL_ARRAY(int, arr, N); | |
| 76 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(IS_INLINE(arr) && "int: boundary size should use inline buffer"); |
| 77 | 1 | } | |
| 78 | |||
| 79 | 1 | static void test_int_just_over_boundary(void) | |
| 80 | { | ||
| 81 | 1 | const size_t N = EXPECTED_CAP(int) + 1; | |
| 82 | 2 | SMALL_ARRAY(int, arr, N); | |
| 83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(arr != NULL); |
| 84 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(!IS_INLINE(arr)); |
| 85 |
2/2✓ Branch 0 taken 257 times.
✓ Branch 1 taken 1 times.
|
258 | for (size_t i = 0; i < N; i++) arr[i] = (int)i; |
| 86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(arr[N-1] == (int)(N-1)); |
| 87 | 1 | } | |
| 88 | |||
| 89 | 1 | static void test_cpuid_512(void) | |
| 90 | { | ||
| 91 | /* By decision, the array in the stack will be able to hold at least 512 CPUs */ | ||
| 92 | 1 | const size_t N = 512; | |
| 93 | 2 | SMALL_ARRAY(cpuid_t, map, N); | |
| 94 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(IS_INLINE(map)); |
| 95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(__map.size == N); |
| 96 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 1 times.
|
513 | for (size_t i = 0; i < N; i++) map[i] = (cpuid_t)i; |
| 97 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 512 times.
✓ Branch 3 taken 512 times.
✓ Branch 4 taken 1 times.
|
513 | for (size_t i = 0; i < N; i++) assert(map[i] == (cpuid_t)i); |
| 98 | 1 | } | |
| 99 | |||
| 100 | 1 | int main(void) { | |
| 101 | |||
| 102 | 1 | printf("SMALL_ARRAY_DEFAULT_BYTES = %d\n\n", | |
| 103 | SMALL_ARRAY_DEFAULT_BYTES); | ||
| 104 | |||
| 105 | 1 | test_zero_size(); | |
| 106 | 1 | test_size_one(); | |
| 107 | 1 | test_int_inline(); | |
| 108 | 1 | test_int_heap(); | |
| 109 | 1 | test_int_boundary(); | |
| 110 | 1 | test_int_just_over_boundary(); | |
| 111 | 1 | test_cpuid_512(); | |
| 112 | |||
| 113 | 1 | return 0; | |
| 114 | } | ||
| 115 |