Skip to content

Commit 06a3d6f

Browse files
committed
ut: Tests added for cm_malloc/free functions
Multiple changes to re-use already written tests for kmalloc/kfree to also test similar cm_malloc/cm_free functions.
1 parent 6d4a8e2 commit 06a3d6f

File tree

10 files changed

+239
-84
lines changed

10 files changed

+239
-84
lines changed

include/cm/cm.h

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,42 @@
2626
***************************************************************************************************/
2727
#define HALT() for (;;)
2828

29-
#define cm_panic() \
30-
do { \
31-
CM_DBG_ERROR ("Panic at %s: %u", __FILE__, __LINE__); \
32-
cm_process_abort (CM_ABORT_EXIT_CODE); \
33-
} while (0)
34-
35-
#define cm_assert(t) \
36-
do { \
37-
if (!(t)) { \
38-
cm_panic(); \
39-
} \
40-
} while (0)
29+
#ifndef UNITTEST
30+
#define cm_panic() \
31+
do { \
32+
CM_DBG_ERROR ("Panic at %s: %u", __FILE__, __LINE__); \
33+
cm_process_abort (CM_ABORT_EXIT_CODE); \
34+
} while (0)
35+
#else // UNITTEST
36+
void cm_unittest_panic_handler();
37+
extern bool cm_panic_invoked;
38+
39+
/* Returns from the 'function under testing', when an assert/panic is hit.
40+
*
41+
* There is x86 assembly hard coded in an arch independent header, however this corresponds to
42+
* the host (not the target) arch. Which implies that unittests can only be built & run on an
43+
* x86 machine.
44+
*
45+
* TODO: Find some way to make this host independent.
46+
* NOTE: EAX is not preserved by GCC. So there is not point adding it to the clobber list.
47+
*/
48+
#define cm_panic() \
49+
do { \
50+
cm_unittest_panic_handler(); \
51+
__asm__ volatile("mov esp, ebp; pop ebp; mov eax, 0; ret;" ::); \
52+
} while (0)
53+
#endif // UNITTEST
54+
55+
#if defined(DEBUG)
56+
#define cm_assert(t) \
57+
do { \
58+
if (!(t)) { \
59+
cm_panic(); \
60+
} \
61+
} while (0)
62+
#else
63+
#define cm_assert(...) (void)0
64+
#endif // DEBUG
4165

4266
void cm_delay (UINT ms);
4367

include/kcmlib.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
* Types and function declarations defined here are private to the CM library and should not be
66
* exposed to the applications.
77
*/
8+
#pragma once
9+
810
#include <intrusive_list.h>
911
#include <memloc.h>
1012

11-
#pragma once
12-
1313
typedef struct CM_MallocHeader
1414
{
1515
size_t netNodeSize; /// Size of a region together with the header size.
@@ -19,7 +19,11 @@ typedef struct CM_MallocHeader
1919
ListNode allocnode; /// A node in the Allocation list
2020
} CM_MallocHeader;
2121

22-
#define CM_MALLOC_MEM_SIZE_BYTES (ARCH_MEM_LEN_BYTES_PROCESS_DATA / 2)
22+
#if defined(UNITTEST)
23+
#define CM_MALLOC_MEM_SIZE_BYTES MOCK_THIS_MACRO_USING (cm_arch_mem_len_bytes_malloc)
24+
#else
25+
#define CM_MALLOC_MEM_SIZE_BYTES (ARCH_MEM_LEN_BYTES_PROCESS_DATA / 2)
26+
#endif
2327

2428
extern uint32_t cm_error_num;
2529

include/mock/cm/cm.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include <unittest/fake.h>
4+
#include <cm/cm.h>
5+
6+
DECLARE_FUNC (void*, cm_memset, void*, U8, size_t);
7+
DECLARE_FUNC (char*, cm_strncpy, char*, const char*, size_t);
8+
DECLARE_FUNC (S32, syscall, U32, U32, U32, U32, U32, U32);
9+
10+
void resetCMFake();

include/mosunittest.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ typedef struct MockedMacro {
2020
uintptr_t arch_mem_start_salloc;
2121
size_t arch_mem_len_bytes_salloc;
2222
size_t arch_mem_len_bytes_kmalloc;
23+
// LibCM
24+
size_t cm_arch_mem_len_bytes_malloc;
2325
} MockedMacro;
2426

2527
// Need to define it when building unittests

src/mock/cm/cm.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <mock/cm/cm.h>
2+
#include <unittest/fake.h>
3+
4+
DEFINE_FUNC (void*, cm_memset, void*, U8, size_t);
5+
DEFINE_FUNC (char*, cm_strncpy, char*, const char*, size_t);
6+
DEFINE_FUNC (S32, syscall, U32, U32, U32, U32, U32, U32);
7+
8+
void resetCMFake()
9+
{
10+
RESET_FAKE(cm_memset);
11+
RESET_FAKE(cm_strncpy);
12+
RESET_FAKE(syscall);
13+
}

src/mock/mockfortests_x86.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ set(kstdlib_mock_sources
1919
set(handles_mock_sources
2020
${PROJECT_SOURCE_DIR}/src/mock/kernel/salloc.c
2121
)
22+
23+
set(cm_malloc_mock_sources
24+
${PROJECT_SOURCE_DIR}/src/mock/cm/cm.c
25+
)

src/unittests/cm/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,15 @@ test(
1919
${PROJECT_SOURCE_DIR}/src/unittests/unittest.c
2020
${CMAKE_CURRENT_SOURCE_DIR}/string_test.c
2121
)
22+
23+
test(
24+
NAME cm_malloc_test
25+
DEPENDENT_FOR build-all
26+
DEFINITIONS LIBCM
27+
SOURCES
28+
${PROJECT_SOURCE_DIR}/src/cm/malloc.c
29+
${CMAKE_CURRENT_SOURCE_DIR}/malloc_test.c
30+
${PROJECT_SOURCE_DIR}/src/unittests/unittest.c
31+
${CMAKE_CURRENT_SOURCE_DIR}/common.c
32+
${cm_malloc_mock_sources}
33+
)

src/unittests/cm/common.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* ----------------------------------------------------------------------------
3+
* Common functions to for use in MeghaOS LibCM unitests
4+
* ----------------------------------------------------------------------------
5+
*/
6+
7+
#include <unittest/unittest.h>
8+
#include <stdio.h>
9+
#include <types.h>
10+
#include <stdarg.h>
11+
#include <kernel.h>
12+
#include <mosunittest.h>
13+
14+
/* MOS Unittest Macro mock global structure */
15+
MockedMacro g_utmm = { 0 };
16+
17+
/* Error number */
18+
uint32_t cm_error_num;
19+
20+
/* Panic handler */
21+
bool cm_panic_invoked;
22+
void cm_unittest_panic_handler()
23+
{
24+
cm_panic_invoked = true;
25+
}

src/unittests/cm/malloc_test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../kernel/kmalloc_test.c

0 commit comments

Comments
 (0)