| 1 |
/* |
| 2 |
* FCML - Free Code Manipulation Library. |
| 3 |
* Copyright (C) 2010-2020 Slawomir Wojtasiak |
| 4 |
* |
| 5 |
* This 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 |
* This 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 this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <fcml_env.h> |
| 21 |
|
| 22 |
#include "env_t.h" |
| 23 |
|
| 24 |
#include <fcml_symbols.h> |
| 25 |
|
| 26 |
#include "fcml_env_int.h" |
| 27 |
|
| 28 |
fcml_int counter = 0; |
| 29 |
|
| 30 |
fcml_int realloc_counter = 0; |
| 31 |
|
| 32 |
fcml_fp_env_memory_alloc_handler default_alloc; |
| 33 |
|
| 34 |
fcml_fp_env_memory_realloc_handler default_realloc; |
| 35 |
|
| 36 |
fcml_fp_env_memory_free_handler default_free; |
| 37 |
|
| 38 |
fcml_ptr fcml_tf_env_memory_alloc_handler_counter( fcml_usize size ) { |
| 39 |
counter += 1; |
| 40 |
return default_alloc( size ); |
| 41 |
} |
| 42 |
|
| 43 |
fcml_ptr fcml_tf_env_memory_realloc_handler_counter( fcml_ptr ptr, fcml_usize size ) { |
| 44 |
realloc_counter += 1; |
| 45 |
return default_realloc( ptr, size ); |
| 46 |
} |
| 47 |
|
| 48 |
void fcml_tf_env_memory_free_handler_counter( fcml_ptr ptr ) { |
| 49 |
counter -= 1; |
| 50 |
default_free( ptr ); |
| 51 |
} |
| 52 |
|
| 53 |
fcml_bool fcml_tf_env_suite_init(void) { |
| 54 |
default_alloc = fcml_fn_env_register_memory_alloc_handler( &fcml_tf_env_memory_alloc_handler_counter ); |
| 55 |
default_realloc = fcml_fn_env_register_memory_realloc_handler( &fcml_tf_env_memory_realloc_handler_counter ); |
| 56 |
default_free = fcml_fn_env_register_memory_free_handler( &fcml_tf_env_memory_free_handler_counter ); |
| 57 |
return FCML_TRUE; |
| 58 |
} |
| 59 |
|
| 60 |
fcml_bool fcml_tf_env_suite_cleanup(void) { |
| 61 |
fcml_fn_env_register_memory_alloc_handler( default_alloc ); |
| 62 |
fcml_fn_env_register_memory_realloc_handler( default_realloc ); |
| 63 |
fcml_fn_env_register_memory_free_handler( default_free ); |
| 64 |
return FCML_TRUE; |
| 65 |
} |
| 66 |
|
| 67 |
void fcml_fn_env_test_memory_alloc(void) { |
| 68 |
fcml_st_symbol *symbol = (fcml_st_symbol*)fcml_fn_symbol_alloc( "TEST", 1 ); |
| 69 |
STF_ASSERT_PTR_NOT_NULL( symbol ); |
| 70 |
if( symbol ) { |
| 71 |
STF_ASSERT_EQUAL( counter, 2 ); // symbol+symbol_name string |
| 72 |
fcml_fn_symbol_free( symbol ); |
| 73 |
STF_ASSERT_EQUAL( counter, 0 ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
void fcml_fn_env_test_memory_realloc(void) { |
| 78 |
|
| 79 |
fcml_ptr block = fcml_fn_env_memory_alloc( 100 ); |
| 80 |
STF_ASSERT_PTR_NOT_NULL( block ); |
| 81 |
if( block ) { |
| 82 |
STF_ASSERT_EQUAL( counter, 1 ); |
| 83 |
fcml_ptr block_realloc = fcml_fn_env_memory_realloc( block, 200 ); |
| 84 |
fcml_fn_env_memory_clear( block_realloc, 200 ); |
| 85 |
STF_ASSERT_PTR_NOT_NULL( block_realloc ); |
| 86 |
STF_ASSERT_EQUAL( realloc_counter, 1 ); |
| 87 |
fcml_fn_symbol_free( block_realloc ); |
| 88 |
STF_ASSERT_EQUAL( counter, 0 ); |
| 89 |
} |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
fcml_stf_test_case fcml_ti_env[] = { |
| 94 |
{ "fcml_fn_env_test_memory_alloc", fcml_fn_env_test_memory_alloc }, |
| 95 |
{ "fcml_fn_env_test_memory_realloc", fcml_fn_env_test_memory_realloc }, |
| 96 |
FCML_STF_NULL_TEST |
| 97 |
}; |
| 98 |
|
| 99 |
fcml_stf_test_suite fcml_si_env = { |
| 100 |
"suite-fcml-env", fcml_tf_env_suite_init, fcml_tf_env_suite_cleanup, fcml_ti_env |
| 101 |
}; |
| 102 |
|
| 103 |
|