1
- /*
2
- * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
3
- * SPDX-License-Identifier: Apache-2.0
1
+
2
+ /* mbed Microcontroller Library
3
+ * Copyright (c) 2018 ARM Limited
4
4
*
5
- * Licensed under the Apache License, Version 2.0 (the "License"); you may
6
- * not use this file except in compliance with the License.
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
7
* You may obtain a copy of the License at
8
8
*
9
- * http://www.apache.org/licenses/LICENSE-2.0
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
10
*
11
11
* Unless required by applicable law or agreed to in writing, software
12
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
14
* See the License for the specific language governing permissions and
15
15
* limitations under the License.
16
16
*/
17
- # include " mbed.h "
17
+
18
18
#include " greentea-client/test_env.h"
19
19
#include " unity/unity.h"
20
20
#include " utest/utest.h"
21
- #include " mbed_stats.h"
22
- #include < stdlib.h>
23
- #include < stdio.h>
21
+
22
+ #include " mbed.h"
24
23
25
24
#if !defined(MBED_THREAD_STATS_ENABLED)
26
25
#warning [NOT_SUPPORTED] test not supported
@@ -53,52 +52,57 @@ void increment_with_delay()
53
52
54
53
void test_case_single_thread_stats ()
55
54
{
55
+ mbed_stats_thread_t *stats = new mbed_stats_thread_t [MAX_THREAD_STATS];
56
+ int old_count = mbed_stats_thread_get_each (stats, MAX_THREAD_STATS);
57
+
56
58
Thread t1 (osPriorityNormal, TEST_STACK_SIZE, NULL , " Th1" );
57
59
t1.start (increment_with_delay);
58
60
59
61
// Read stats
60
- mbed_stats_thread_t *stats = new mbed_stats_thread_t [MAX_THREAD_STATS];
61
62
int count = mbed_stats_thread_get_each (stats, MAX_THREAD_STATS);
62
- TEST_ASSERT_EQUAL (4 , count);
63
+ TEST_ASSERT_EQUAL (1 , ( count-old_count) );
63
64
64
- for (int i = 0 ; i < count; i++) {
65
- if (0 == strcmp (stats[i].thread_name , " Th1" )) {
65
+ for (int i = 0 ; i < count; i++) {
66
+ if (0 == strcmp (stats[i].thread_name , " Th1" )) {
66
67
TEST_ASSERT_EQUAL (TEST_STACK_SIZE, stats[i].thread_stack_size );
67
68
TEST_ASSERT_EQUAL (osPriorityNormal, stats[i].thread_priority );
68
69
break ;
69
70
}
70
71
}
71
- delete[] stats;
72
+
72
73
t1.terminate ();
74
+ delete[] stats;
73
75
}
74
76
77
+ #define SINGLE_ELEMENT 1
75
78
void test_case_less_count ()
76
79
{
77
80
// Default Mbed OS has 3 threads
78
- mbed_stats_thread_t *stats = new mbed_stats_thread_t [2 ];
79
- int count = mbed_stats_thread_get_each (stats, 2 );
80
- TEST_ASSERT_EQUAL (2 , count);
81
- delete[] stats;
81
+ mbed_stats_thread_t stats;
82
+ int count = mbed_stats_thread_get_each (&stats, SINGLE_ELEMENT);
83
+ TEST_ASSERT_EQUAL (SINGLE_ELEMENT, count);
82
84
}
83
85
84
86
void test_case_multi_threads_blocked ()
85
87
{
88
+ mbed_stats_thread_t *stats = new mbed_stats_thread_t [MAX_THREAD_STATS];
89
+ int old_count = mbed_stats_thread_get_each (stats, MAX_THREAD_STATS);
90
+
86
91
Thread t1 (osPriorityNormal, TEST_STACK_SIZE, NULL , " Th1" );
87
92
Thread t2 (osPriorityNormal1, TEST_STACK_SIZE, NULL , " Th2" );
88
93
t1.start (increment_with_delay);
89
94
t2.start (decrement_on_event);
90
95
91
96
// Read stats
92
- mbed_stats_thread_t *stats = new mbed_stats_thread_t [MAX_THREAD_STATS];
93
- int count = mbed_stats_thread_get_each (stats, MAX_THREAD_STATS);
94
- TEST_ASSERT_EQUAL (5 , count);
95
97
96
- for (int i = 0 ; i < count; i++) {
97
- if (0 == strcmp (stats[i].thread_name , " Th2" )) {
98
+ int count = mbed_stats_thread_get_each (stats, MAX_THREAD_STATS);
99
+ TEST_ASSERT_EQUAL (2 , (count-old_count));
100
+ for (int i = 0 ; i < count; i++) {
101
+ if (0 == strcmp (stats[i].thread_name , " Th2" )) {
98
102
TEST_ASSERT_EQUAL (TEST_STACK_SIZE, stats[i].thread_stack_size );
99
103
TEST_ASSERT_EQUAL (osPriorityNormal1, stats[i].thread_priority );
100
104
TEST_ASSERT_EQUAL (osThreadBlocked, stats[i].thread_state );
101
- } else if (0 == strcmp (stats[i].thread_name , " Th1" )) {
105
+ } else if (0 == strcmp (stats[i].thread_name , " Th1" )) {
102
106
TEST_ASSERT_EQUAL (TEST_STACK_SIZE, stats[i].thread_stack_size );
103
107
TEST_ASSERT_EQUAL (osPriorityNormal, stats[i].thread_priority );
104
108
}
@@ -111,29 +115,32 @@ void test_case_multi_threads_blocked()
111
115
Thread::wait (100 );
112
116
113
117
count = mbed_stats_thread_get_each (stats, MAX_THREAD_STATS);
114
- TEST_ASSERT_EQUAL (4 , count);
118
+ TEST_ASSERT_EQUAL (1 , ( count-old_count) );
115
119
116
- delete[] stats;
117
120
t1.terminate ();
121
+ delete[] stats;
118
122
}
119
123
120
124
void test_case_multi_threads_terminate ()
121
125
{
126
+ mbed_stats_thread_t *stats = new mbed_stats_thread_t [MAX_THREAD_STATS];
127
+ int old_count = mbed_stats_thread_get_each (stats, MAX_THREAD_STATS);
128
+
122
129
Thread t1 (osPriorityNormal1, TEST_STACK_SIZE, NULL , " Th1" );
123
130
Thread t2 (osPriorityNormal2, TEST_STACK_SIZE, NULL , " Th2" );
124
131
t2.start (increment_with_delay);
125
132
t1.start (decrement_on_event);
126
133
127
134
// Read stats
128
- mbed_stats_thread_t *stats = new mbed_stats_thread_t [MAX_THREAD_STATS];
135
+
129
136
int count = mbed_stats_thread_get_each (stats, MAX_THREAD_STATS);
130
- TEST_ASSERT_EQUAL (5 , count);
137
+ TEST_ASSERT_EQUAL (2 , ( count-old_count) );
131
138
132
- for (int i = 0 ; i < count; i++) {
133
- if (0 == strcmp (stats[i].thread_name , " Th2" )) {
139
+ for (int i = 0 ; i < count; i++) {
140
+ if (0 == strcmp (stats[i].thread_name , " Th2" )) {
134
141
TEST_ASSERT_EQUAL (TEST_STACK_SIZE, stats[i].thread_stack_size );
135
142
TEST_ASSERT_EQUAL (osPriorityNormal2, stats[i].thread_priority );
136
- } else if (0 == strcmp (stats[i].thread_name , " Th1" )) {
143
+ } else if (0 == strcmp (stats[i].thread_name , " Th1" )) {
137
144
TEST_ASSERT_EQUAL (TEST_STACK_SIZE, stats[i].thread_stack_size );
138
145
TEST_ASSERT_EQUAL (osPriorityNormal1, stats[i].thread_priority );
139
146
TEST_ASSERT_EQUAL (osThreadBlocked, stats[i].thread_state );
@@ -144,10 +151,9 @@ void test_case_multi_threads_terminate()
144
151
t2.terminate ();
145
152
146
153
count = mbed_stats_thread_get_each (stats, MAX_THREAD_STATS);
147
- TEST_ASSERT_EQUAL (3 , count );
154
+ TEST_ASSERT_EQUAL (count, old_count );
148
155
149
156
delete[] stats;
150
- t1.terminate ();
151
157
}
152
158
153
159
Case cases[] = {
0 commit comments