Skip to content

Commit bb8ccbd

Browse files
author
deepikabhavnani
committed
Checking difference in thread count + review comments
1 parent 1dad739 commit bb8ccbd

File tree

1 file changed

+42
-36
lines changed
  • TESTS/mbed_platform/stats_thread

1 file changed

+42
-36
lines changed

TESTS/mbed_platform/stats_thread/main.cpp

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
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
44
*
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.
77
* You may obtain a copy of the License at
88
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
9+
* http://www.apache.org/licenses/LICENSE-2.0
1010
*
1111
* 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.
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
#include "mbed.h"
17+
1818
#include "greentea-client/test_env.h"
1919
#include "unity/unity.h"
2020
#include "utest/utest.h"
21-
#include "mbed_stats.h"
22-
#include <stdlib.h>
23-
#include <stdio.h>
21+
22+
#include "mbed.h"
2423

2524
#if !defined(MBED_THREAD_STATS_ENABLED)
2625
#warning [NOT_SUPPORTED] test not supported
@@ -53,52 +52,57 @@ void increment_with_delay()
5352

5453
void test_case_single_thread_stats()
5554
{
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+
5658
Thread t1(osPriorityNormal, TEST_STACK_SIZE, NULL, "Th1");
5759
t1.start(increment_with_delay);
5860

5961
// Read stats
60-
mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS];
6162
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));
6364

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")) {
6667
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].thread_stack_size);
6768
TEST_ASSERT_EQUAL(osPriorityNormal, stats[i].thread_priority);
6869
break;
6970
}
7071
}
71-
delete[] stats;
72+
7273
t1.terminate();
74+
delete[] stats;
7375
}
7476

77+
#define SINGLE_ELEMENT 1
7578
void test_case_less_count()
7679
{
7780
// 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);
8284
}
8385

8486
void test_case_multi_threads_blocked()
8587
{
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+
8691
Thread t1(osPriorityNormal, TEST_STACK_SIZE, NULL, "Th1");
8792
Thread t2(osPriorityNormal1, TEST_STACK_SIZE, NULL, "Th2");
8893
t1.start(increment_with_delay);
8994
t2.start(decrement_on_event);
9095

9196
// 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);
9597

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")) {
98102
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].thread_stack_size);
99103
TEST_ASSERT_EQUAL(osPriorityNormal1, stats[i].thread_priority);
100104
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")) {
102106
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].thread_stack_size);
103107
TEST_ASSERT_EQUAL(osPriorityNormal, stats[i].thread_priority);
104108
}
@@ -111,29 +115,32 @@ void test_case_multi_threads_blocked()
111115
Thread::wait(100);
112116

113117
count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
114-
TEST_ASSERT_EQUAL(4, count);
118+
TEST_ASSERT_EQUAL(1, (count-old_count));
115119

116-
delete[] stats;
117120
t1.terminate();
121+
delete[] stats;
118122
}
119123

120124
void test_case_multi_threads_terminate()
121125
{
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+
122129
Thread t1(osPriorityNormal1, TEST_STACK_SIZE, NULL, "Th1");
123130
Thread t2(osPriorityNormal2, TEST_STACK_SIZE, NULL, "Th2");
124131
t2.start(increment_with_delay);
125132
t1.start(decrement_on_event);
126133

127134
// Read stats
128-
mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS];
135+
129136
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));
131138

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")) {
134141
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].thread_stack_size);
135142
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")) {
137144
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].thread_stack_size);
138145
TEST_ASSERT_EQUAL(osPriorityNormal1, stats[i].thread_priority);
139146
TEST_ASSERT_EQUAL(osThreadBlocked, stats[i].thread_state);
@@ -144,10 +151,9 @@ void test_case_multi_threads_terminate()
144151
t2.terminate();
145152

146153
count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
147-
TEST_ASSERT_EQUAL(3, count);
154+
TEST_ASSERT_EQUAL(count, old_count);
148155

149156
delete[] stats;
150-
t1.terminate();
151157
}
152158

153159
Case cases[] = {

0 commit comments

Comments
 (0)