Debian Codegraph @

u8stream/test/unit/universal_memory_arena_test.c

raw symbols SCIP
1 /* File: universal_memory_arena_test.c; Copyright and License: see below */
2
3 #include "universal_memory_arena_test.h"
4 #include "u8arena/universal_memory_arena.h"
5 #include "test_fixture.h"
6 #include "test_expect.h"
7 #include "test_environment_assert.h"
8 #include "test_case_result.h"
9 #include <string.h>
10 #include <stdint.h>
11 #include <assert.h>
12
13 static test_fixture_t * set_up();
14 static void tear_down( test_fixture_t *fix );
15 static test_case_result_t test_alloc_blocks( test_fixture_t *fix );
16
17 test_suite_t universal_memory_arena_test_get_suite(void)
18 {
19 test_suite_t result;
20 test_suite_init( &result,
21 "universal_memory_arena_test_get_suite",
22 TEST_CATEGORY_UNIT | TEST_CATEGORY_CONTINUOUS | TEST_CATEGORY_COVERAGE,
23 &set_up,
24 &tear_down
25 );
26 test_suite_add_test_case( &result, "test_alloc_blocks", &test_alloc_blocks );
27 return result;
28 }
29
30 static test_fixture_t * set_up()
31 {
32 return NULL;
33 }
34
35 static void tear_down( test_fixture_t *fix )
36 {
37 }
38
39 static test_case_result_t test_alloc_blocks( test_fixture_t *fix )
40 {
41 char thirty_three[33];
42
43 universal_memory_arena_t test_me;
44 universal_memory_arena_init( &test_me, &(thirty_three[1]), sizeof(thirty_three)-1 );
45
46 void *block_1;
47 u8_error_t err;
48 err = universal_memory_arena_get_block( &test_me, 1, &block_1 );
49 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, err );
50 TEST_EXPECT( NULL != block_1 );
51 TEST_EXPECT( &(thirty_three[1]) == block_1 );
52
53 void *block_2;
54 err = universal_memory_arena_get_block( &test_me, 1, &block_2 );
55 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, err );
56 TEST_EXPECT( NULL != block_2 );
57 TEST_EXPECT( &(thirty_three[1+sizeof(int)]) == block_2 );
58
59 universal_memory_arena_reset( &test_me );
60
61 void *block_3;
62 err = universal_memory_arena_get_block( &test_me, 33-sizeof(int), &block_3 );
63 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, err );
64 TEST_EXPECT( NULL != block_3 );
65 TEST_EXPECT( &(thirty_three[1]) == block_3 );
66
67 void *block_4;
68 err = universal_memory_arena_get_block( &test_me, sizeof(int), &block_4 );
69 TEST_EXPECT_EQUAL_INT( U8_ERROR_ARRAY_BUFFER_EXCEEDED, err );
70 TEST_EXPECT( NULL == block_4 );
71
72 universal_memory_arena_destroy( &test_me );
73 return TEST_CASE_RESULT_OK;
74 }
75
76
77 /*
78 * Copyright 2021-2026 Andreas Warnke
79 *
80 * Licensed under the Apache License, Version 2.0 (the "License");
81 * you may not use this file except in compliance with the License.
82 * You may obtain a copy of the License at
83 *
84 * http://www.apache.org/licenses/LICENSE-2.0
85 *
86 * Unless required by applicable law or agreed to in writing, software
87 * distributed under the License is distributed on an "AS IS" BASIS,
88 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
89 * See the License for the specific language governing permissions and
90 * limitations under the License.
91 */