Morse Micro IoT SDK  2.9.7
test_os.c
1/*
2 * Copyright 2023 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#include "porting_assistant.h"
8
9TEST_STEP(test_step_os_malloc, "Memory allocation")
10{
11 void *buf = mmosal_malloc(1560);
12 if (buf == NULL)
13 {
14 TEST_LOG_APPEND(
15 "Failed to allocate 1560 bytes. Check that your heap is configured correctly\n\n");
16 return TEST_FAILED;
17 }
19 return TEST_PASSED;
20}
21
22TEST_STEP(test_step_os_realloc, "Memory reallocation")
23{
24 enum constants
25 {
26 FIRST_ALLOCATION_SIZE = 100,
27 REALLOCATION_SIZE = 200,
28 };
29
30 uint8_t *buf = (uint8_t *)mmosal_malloc(FIRST_ALLOCATION_SIZE);
31 if (buf == NULL)
32 {
33 TEST_LOG_APPEND(
34 "Failed to allocate %d bytes. Check that your heap is configured correctly\n\n",
35 FIRST_ALLOCATION_SIZE);
36 return TEST_FAILED_NON_CRITICAL;
37 }
38 memset(buf, 0xc0, FIRST_ALLOCATION_SIZE);
39
40 uint8_t *buf1 = (uint8_t *)mmosal_realloc(buf, REALLOCATION_SIZE);
41 if (buf1 == NULL)
42 {
43 TEST_LOG_APPEND(
44 "Failed to reallocate %d bytes. Check that your heap supports realloc\n\n",
45 REALLOCATION_SIZE);
46 return TEST_FAILED_NON_CRITICAL;
47 }
48
49 /* Verify the reallocated memory contains the contents of the original block. */
50 unsigned ii;
51 for (ii = 0; ii < FIRST_ALLOCATION_SIZE; ii++)
52 {
53 if (buf1[ii] != 0xc0)
54 {
55 TEST_LOG_APPEND(
56 "Reallocated block contents mismatch at offset %u\n\n", ii);
57 return TEST_FAILED_NON_CRITICAL;
58 }
59 }
60
61 mmosal_free(buf1);
62 return TEST_PASSED;
63}
64
65TEST_STEP(test_step_os_time, "Passage of time")
66{
67 uint32_t start_time = mmosal_get_time_ms();
69 uint32_t end_time = mmosal_get_time_ms();
70
71 int32_t delta = (int32_t)(end_time - start_time);
72 if (delta < 49 || delta > 51)
73 {
74 TEST_LOG_APPEND(
75 "Time delta (%ld ms) did not match sleep time (50 ms)\n\n", delta);
76 return TEST_FAILED;
77 }
78
79 return TEST_PASSED;
80}
81
84enum task_state
85{
86 TASK_NOT_STARTED,
87 TASK_STARTED,
88 TASK_TERMINATING,
89 TASK_ERROR_GET_ACTIVE_INVALID,
90};
91
93static volatile enum task_state task_state = TASK_NOT_STARTED;
95static struct mmosal_task * volatile task_handle;
96
98static void new_task_main(void *arg)
99{
100 (void)arg;
101
102 task_state = TASK_STARTED;
103
104 /* Sleep for 10 ms; this should yield the task. */
106
107 /* Verify mmosal_task_get_active() returns the correct task handle. */
108 if (mmosal_task_get_active() != task_handle)
109 {
110 task_state = TASK_ERROR_GET_ACTIVE_INVALID;
111 return;
112 }
113
114 task_state = TASK_TERMINATING;
115
116 /* Delete self */
117 mmosal_task_delete(NULL);
118}
119
120TEST_STEP(test_step_os_task_creation, "Task creation and preemption")
121{
122 task_handle = mmosal_task_create(
123 new_task_main, NULL, MMOSAL_TASK_PRI_HIGH, 512, "Test Task");
124 if (task_handle == NULL)
125 {
126 TEST_LOG_APPEND(
127 "mmosal_task_create() returned NULL; expected a task handle.\n\n");
128 return TEST_FAILED_NON_CRITICAL;
129 }
130
131 /*
132 * The newly created task should be higher priority. Therefore the following code should
133 * not run until after the task yields.
134 */
135 if (task_state != TASK_STARTED)
136 {
137 TEST_LOG_APPEND(
138 "The task created with mmosal_task_create() did not run.\n\n");
139 return TEST_FAILED_NON_CRITICAL;
140 }
141
142 /* Allow some time for the task to wake up. */
144
145 switch (task_state)
146 {
147 case TASK_TERMINATING:
148 break;
149
150 case TASK_ERROR_GET_ACTIVE_INVALID:
151 TEST_LOG_APPEND(
152 "mmosal_task_get_active() did not return the correct task handle.\n\n");
153 return TEST_FAILED_NON_CRITICAL;
154
155 default:
156 TEST_LOG_APPEND(
157 "Task in unexpected state %d.\n\n", task_state);
158 return TEST_FAILED_NON_CRITICAL;
159 }
160
161 return TEST_PASSED;
162}
#define mmosal_malloc(size)
Allocate memory of the given size and return a pointer to it (malloc).
Definition: mmosal.h:137
void mmosal_free(void *p)
Free the given memory allocation.
void * mmosal_realloc(void *ptr, size_t size)
Equivalent of standard library realloc().
struct mmosal_task * mmosal_task_create(mmosal_task_fn_t task_fn, void *argument, enum mmosal_task_priority priority, unsigned stack_size_u32, const char *name)
Create a new task.
struct mmosal_task * mmosal_task_get_active(void)
Get the handle of the active task.
void mmosal_task_sleep(uint32_t duration_ms)
Sleep for a period of time, yielding during that time.
void mmosal_task_delete(struct mmosal_task *task)
Delete the given task.
@ MMOSAL_TASK_PRI_HIGH
High priority.
Definition: mmosal.h:187
uint32_t mmosal_get_time_ms(void)
Get the system time in milliseconds.
const struct test_step test_step_os_malloc
Test definition.
const struct test_step test_step_os_realloc
Test definition.
const struct test_step test_step_os_time
Test definition.
const struct test_step test_step_os_task_creation
Test definition.
char buf[1408]
Statically allocated buffer for HTTP GET request, just under 1 packet size.
Definition: sslclient.c:177