Skip to content

Commit 170268f

Browse files
authored
Merge pull request #291 from ARMmbed/devel_gt_client
[test porting] Merge mbedmicro tests (HAL, RTOS and basic NET) to mbed-os/TESTS
2 parents a326c13 + bc46495 commit 170268f

File tree

35 files changed

+2845
-0
lines changed

35 files changed

+2845
-0
lines changed

TESTS/mbed_drivers/c_strings/main.cpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
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.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
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.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#include <stdio.h>
18+
#include <string.h>
19+
#include "mbed.h"
20+
#include "greentea-client/test_env.h"
21+
#include "unity/unity.h"
22+
#include "utest/utest.h"
23+
24+
namespace {
25+
static char buffer[256] = {0};
26+
}
27+
28+
#define CLEAN_BUFFER memset(::buffer, 0x00, sizeof(::buffer))
29+
#define NEGATIVE_INTEGERS -32768,-3214,-999,-100,-1,0,-1,-4231,-999,-4123,-32760,-99999
30+
#define POSITIVE_INTEGERS 32768,3214,999,100,1,0,1,4231,999,4123,32760,99999
31+
#define FLOATS 0.002,0.92430,15.91320,791.77368,6208.2,25719.4952,426815.982588,6429271.046,42468024.93,212006462.910
32+
33+
using namespace utest::v1;
34+
35+
36+
void test_case_c_string_i_d() {
37+
CLEAN_BUFFER;
38+
sprintf(buffer, "%i %d %i %d %i %d %i %d %i %d %i %i", NEGATIVE_INTEGERS);
39+
TEST_ASSERT_EQUAL_STRING("-32768 -3214 -999 -100 -1 0 -1 -4231 -999 -4123 -32760 -99999", buffer);
40+
}
41+
42+
void test_case_c_string_u_d() {
43+
CLEAN_BUFFER;
44+
sprintf(buffer, "%u %d %u %d %u %d %u %d %u %d %u %d", POSITIVE_INTEGERS);
45+
TEST_ASSERT_EQUAL_STRING("32768 3214 999 100 1 0 1 4231 999 4123 32760 99999", buffer);
46+
}
47+
48+
void test_case_c_string_x_E() {
49+
CLEAN_BUFFER;
50+
sprintf(buffer, "%x %X %x %X %x %X %x %X %x %X %x %X", POSITIVE_INTEGERS);
51+
TEST_ASSERT_EQUAL_STRING("8000 C8E 3e7 64 1 0 1 1087 3e7 101B 7ff8 1869F", buffer);
52+
}
53+
54+
void test_case_c_string_f_f() {
55+
CLEAN_BUFFER;
56+
sprintf(buffer, "%f %f %f %f %f %f %f %f %f %f", FLOATS);
57+
TEST_ASSERT_EQUAL_STRING("0.002000 0.924300 15.913200 791.773680 6208.200000 25719.495200 426815.982588 6429271.046000 42468024.930000 212006462.910000", buffer);
58+
}
59+
60+
void test_case_c_string_g_g() {
61+
CLEAN_BUFFER;
62+
sprintf(buffer, "%g %g %g %g %g %g %g %g %g %g", FLOATS);
63+
TEST_ASSERT_EQUAL_STRING("0.002 0.9243 15.9132 791.774 6208.2 25719.5 426816 6.42927e+006 4.2468e+007 2.12006e+008", buffer);
64+
TEST_ASSERT_EQUAL_STRING("0.002 0.9243 15.9132 791.774 6208.2 25719.5 426816 6.42927e+06 4.2468e+07 2.12006e+08", buffer);
65+
}
66+
67+
void test_case_c_string_e_E() {
68+
CLEAN_BUFFER;
69+
sprintf(buffer, "%e %E %e %E %e %E %e %E %e %E", FLOATS);
70+
TEST_ASSERT_EQUAL_STRING("2.000000e-003 9.243000E-001 1.591320e+001 7.917737E+002 6.208200e+003 2.571950E+004 4.268160e+005 6.429271E+006 4.246802e+007 2.120065E+008", buffer);
71+
TEST_ASSERT_EQUAL_STRING("2.000000e-03 9.243000E-01 1.591320e+01 7.917737E+02 6.208200e+03 2.571950E+04 4.268160e+05 6.429271E+06 4.246802e+07 2.120065E+08", buffer);
72+
}
73+
74+
void test_case_c_string_strtok() {
75+
CLEAN_BUFFER;
76+
char str[] ="- This, a sample string.";
77+
char * pch = strtok (str," ,.-");
78+
while (pch != NULL) {
79+
strcat(buffer, pch);
80+
pch = strtok (NULL, " ,.-");
81+
}
82+
TEST_ASSERT_EQUAL_STRING("Thisasamplestring", buffer);
83+
}
84+
85+
void test_case_c_string_strpbrk() {
86+
CLEAN_BUFFER;
87+
char str[] = "This is a sample string";
88+
char key[] = "aeiou";
89+
char *pch = strpbrk(str, key);
90+
while (pch != NULL)
91+
{
92+
char buf[2] = {*pch, '\0'};
93+
strcat(buffer, buf);
94+
pch = strpbrk(pch + 1,key);
95+
}
96+
TEST_ASSERT_EQUAL_STRING("iiaaei", buffer);
97+
}
98+
99+
utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
100+
greentea_case_failure_abort_handler(source, reason);
101+
return STATUS_CONTINUE;
102+
}
103+
104+
Case cases[] = {
105+
Case("C strings: strtok", test_case_c_string_strtok, greentea_failure_handler),
106+
Case("C strings: strpbrk", test_case_c_string_strpbrk, greentea_failure_handler),
107+
Case("C strings: %i %d integer formatting", test_case_c_string_i_d, greentea_failure_handler),
108+
Case("C strings: %u %d integer formatting", test_case_c_string_u_d, greentea_failure_handler),
109+
Case("C strings: %x %E integer formatting", test_case_c_string_x_E, greentea_failure_handler),
110+
Case("C strings: %f %f float formatting", test_case_c_string_f_f, greentea_failure_handler),
111+
Case("C strings: %e %E float formatting", test_case_c_string_e_E, greentea_failure_handler),
112+
Case("C strings: %g %g float formatting", test_case_c_string_g_g, greentea_failure_handler),
113+
};
114+
115+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
116+
GREENTEA_SETUP(5, "default_auto");
117+
return greentea_test_setup_handler(number_of_cases);
118+
}
119+
120+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
121+
122+
int main() {
123+
Harness::run(specification);
124+
}

TESTS/mbed_drivers/callback/main.cpp

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
#include "mbed.h"
2+
#include "greentea-client/test_env.h"
3+
#include "unity.h"
4+
#include "utest.h"
5+
6+
using namespace utest::v1;
7+
8+
9+
// static functions
10+
template <typename T>
11+
T static_func5(T a0, T a1, T a2, T a3, T a4) { return a0 | a1 | a2 | a3 | a4; }
12+
template <typename T>
13+
T static_func4(T a0, T a1, T a2, T a3) { return a0 | a1 | a2 | a3; }
14+
template <typename T>
15+
T static_func3(T a0, T a1, T a2) { return a0 | a1 | a2; }
16+
template <typename T>
17+
T static_func2(T a0, T a1) { return a0 | a1; }
18+
template <typename T>
19+
T static_func1(T a0) { return a0; }
20+
template <typename T>
21+
T static_func0() { return 0; }
22+
23+
// class functions
24+
template <typename T>
25+
struct Thing {
26+
T t;
27+
Thing() : t(0x80) {}
28+
29+
T member_func5(T a0, T a1, T a2, T a3, T a4) { return t | a0 | a1 | a2 | a3 | a4; }
30+
T member_func4(T a0, T a1, T a2, T a3) { return t | a0 | a1 | a2 | a3; }
31+
T member_func3(T a0, T a1, T a2) { return t | a0 | a1 | a2; }
32+
T member_func2(T a0, T a1) { return t | a0 | a1; }
33+
T member_func1(T a0) { return t | a0; }
34+
T member_func0() { return t; }
35+
};
36+
37+
// bound functions
38+
template <typename T>
39+
T bound_func5(Thing<T> *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; }
40+
template <typename T>
41+
T bound_func4(Thing<T> *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; }
42+
template <typename T>
43+
T bound_func3(Thing<T> *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; }
44+
template <typename T>
45+
T bound_func2(Thing<T> *t, T a0, T a1) { return t->t | a0 | a1; }
46+
template <typename T>
47+
T bound_func1(Thing<T> *t, T a0) { return t->t | a0; }
48+
template <typename T>
49+
T bound_func0(Thing<T> *t) { return t->t; }
50+
51+
52+
// function call and result verification
53+
template <typename T>
54+
struct Verifier {
55+
static void verify5(Callback<T(T,T,T,T,T)> func) {
56+
T result = func(0x01, 0x02, 0x04, 0x08, 0x10);
57+
TEST_ASSERT_EQUAL(result, 0x1f);
58+
}
59+
60+
template <typename O, typename M>
61+
static void verify5(O *obj, M method) {
62+
Callback<T(T,T,T,T,T)> func(obj, method);
63+
T result = func(0x01, 0x02, 0x04, 0x08, 0x10);
64+
TEST_ASSERT_EQUAL(result, 0x9f);
65+
}
66+
67+
static void verify4(Callback<T(T,T,T,T)> func) {
68+
T result = func(0x01, 0x02, 0x04, 0x08);
69+
TEST_ASSERT_EQUAL(result, 0x0f);
70+
}
71+
72+
template <typename O, typename M>
73+
static void verify4(O *obj, M method) {
74+
Callback<T(T,T,T,T)> func(obj, method);
75+
T result = func(0x01, 0x02, 0x04, 0x08);
76+
TEST_ASSERT_EQUAL(result, 0x8f);
77+
}
78+
79+
static void verify3(Callback<T(T,T,T)> func) {
80+
T result = func(0x01, 0x02, 0x04);
81+
TEST_ASSERT_EQUAL(result, 0x07);
82+
}
83+
84+
template <typename O, typename M>
85+
static void verify3(O *obj, M method) {
86+
Callback<T(T,T,T)> func(obj, method);
87+
T result = func(0x01, 0x02, 0x04);
88+
TEST_ASSERT_EQUAL(result, 0x87);
89+
}
90+
91+
static void verify2(Callback<T(T,T)> func) {
92+
T result = func(0x01, 0x02);
93+
TEST_ASSERT_EQUAL(result, 0x03);
94+
}
95+
96+
template <typename O, typename M>
97+
static void verify2(O *obj, M method) {
98+
Callback<T(T,T)> func(obj, method);
99+
T result = func(0x01, 0x02);
100+
TEST_ASSERT_EQUAL(result, 0x83);
101+
}
102+
103+
static void verify1(Callback<T(T)> func) {
104+
T result = func(0x01);
105+
TEST_ASSERT_EQUAL(result, 0x01);
106+
}
107+
108+
template <typename O, typename M>
109+
static void verify1(O *obj, M method) {
110+
Callback<T(T)> func(obj, method);
111+
T result = func(0x01);
112+
TEST_ASSERT_EQUAL(result, 0x81);
113+
}
114+
115+
static void verify0(Callback<T()> func) {
116+
T result = func();
117+
TEST_ASSERT_EQUAL(result, 0x00);
118+
}
119+
120+
template <typename O, typename M>
121+
static void verify0(O *obj, M method) {
122+
Callback<T()> func(obj, method);
123+
T result = func();
124+
TEST_ASSERT_EQUAL(result, 0x80);
125+
}
126+
};
127+
128+
129+
// test dispatch
130+
template <typename T>
131+
void test_dispatch5() {
132+
Thing<T> thing;
133+
Verifier<T>::verify5(static_func5<T>);
134+
Verifier<T>::verify5(&thing, &Thing<T>::member_func5);
135+
Verifier<T>::verify5(&thing, &bound_func5<T>);
136+
137+
Callback<T(T,T,T,T,T)> callback(static_func5);
138+
Verifier<T>::verify5(callback);
139+
callback.attach(&thing, &bound_func5<T>);
140+
Verifier<T>::verify5(&callback, &Callback<T(T,T,T,T,T)>::call);
141+
Verifier<T>::verify5((void*)&callback, &Callback<T(T,T,T,T,T)>::thunk);
142+
}
143+
144+
template <typename T>
145+
void test_dispatch4() {
146+
Thing<T> thing;
147+
Verifier<T>::verify4(static_func4<T>);
148+
Verifier<T>::verify4(&thing, &Thing<T>::member_func4);
149+
Verifier<T>::verify4(&thing, &bound_func4<T>);
150+
151+
Callback<T(T,T,T,T)> callback(static_func4);
152+
Verifier<T>::verify4(callback);
153+
callback.attach(&thing, &bound_func4<T>);
154+
Verifier<T>::verify4(&callback, &Callback<T(T,T,T,T)>::call);
155+
Verifier<T>::verify4((void*)&callback, &Callback<T(T,T,T,T)>::thunk);
156+
}
157+
158+
template <typename T>
159+
void test_dispatch3() {
160+
Thing<T> thing;
161+
Verifier<T>::verify3(static_func3<T>);
162+
Verifier<T>::verify3(&thing, &Thing<T>::member_func3);
163+
Verifier<T>::verify3(&thing, &bound_func3<T>);
164+
165+
Callback<T(T,T,T)> callback(static_func3);
166+
Verifier<T>::verify3(callback);
167+
callback.attach(&thing, &bound_func3<T>);
168+
Verifier<T>::verify3(&callback, &Callback<T(T,T,T)>::call);
169+
Verifier<T>::verify3((void*)&callback, &Callback<T(T,T,T)>::thunk);
170+
}
171+
172+
template <typename T>
173+
void test_dispatch2() {
174+
Thing<T> thing;
175+
Verifier<T>::verify2(static_func2<T>);
176+
Verifier<T>::verify2(&thing, &Thing<T>::member_func2);
177+
Verifier<T>::verify2(&thing, &bound_func2<T>);
178+
179+
Callback<T(T,T)> callback(static_func2);
180+
Verifier<T>::verify2(callback);
181+
callback.attach(&thing, &bound_func2<T>);
182+
Verifier<T>::verify2(&callback, &Callback<T(T,T)>::call);
183+
Verifier<T>::verify2((void*)&callback, &Callback<T(T,T)>::thunk);
184+
}
185+
186+
template <typename T>
187+
void test_dispatch1() {
188+
Thing<T> thing;
189+
Verifier<T>::verify1(static_func1<T>);
190+
Verifier<T>::verify1(&thing, &Thing<T>::member_func1);
191+
Verifier<T>::verify1(&thing, &bound_func1<T>);
192+
193+
Callback<T(T)> callback(static_func1);
194+
Verifier<T>::verify1(callback);
195+
callback.attach(&thing, &bound_func1<T>);
196+
Verifier<T>::verify1(&callback, &Callback<T(T)>::call);
197+
Verifier<T>::verify1((void*)&callback, &Callback<T(T)>::thunk);
198+
}
199+
200+
template <typename T>
201+
void test_dispatch0() {
202+
Thing<T> thing;
203+
Verifier<T>::verify0(static_func0<T>);
204+
Verifier<T>::verify0(&thing, &Thing<T>::member_func0);
205+
Verifier<T>::verify0(&thing, &bound_func0<T>);
206+
207+
Callback<T()> callback(static_func0);
208+
Verifier<T>::verify0(callback);
209+
callback.attach(&thing, &bound_func0<T>);
210+
Verifier<T>::verify0(&callback, &Callback<T()>::call);
211+
Verifier<T>::verify0((void*)&callback, &Callback<T()>::thunk);
212+
}
213+
214+
template <typename T>
215+
void test_fparg1() {
216+
Thing<T> thing;
217+
FunctionPointerArg1<T,T> fp(static_func1<T>);
218+
Verifier<T>::verify1(fp);
219+
Verifier<T>::verify1(fp.get_function());
220+
}
221+
222+
template <typename T>
223+
void test_fparg0() {
224+
Thing<T> thing;
225+
FunctionPointerArg1<T,void> fp(static_func0<T>);
226+
Verifier<T>::verify0(fp);
227+
Verifier<T>::verify0(fp.get_function());
228+
}
229+
230+
231+
// Test setup
232+
utest::v1::status_t test_setup(const size_t number_of_cases) {
233+
GREENTEA_SETUP(5, "default_auto");
234+
return verbose_test_setup_handler(number_of_cases);
235+
}
236+
237+
Case cases[] = {
238+
Case("Testing callbacks with 5 ints", test_dispatch5<int>),
239+
Case("Testing callbacks with 4 ints", test_dispatch4<int>),
240+
Case("Testing callbacks with 3 ints", test_dispatch3<int>),
241+
Case("Testing callbacks with 2 ints", test_dispatch2<int>),
242+
Case("Testing callbacks with 1 ints", test_dispatch1<int>),
243+
Case("Testing callbacks with 0 ints", test_dispatch0<int>),
244+
245+
Case("Testing callbacks with 5 uchars", test_dispatch5<unsigned char>),
246+
Case("Testing callbacks with 4 uchars", test_dispatch4<unsigned char>),
247+
Case("Testing callbacks with 3 uchars", test_dispatch3<unsigned char>),
248+
Case("Testing callbacks with 2 uchars", test_dispatch2<unsigned char>),
249+
Case("Testing callbacks with 1 uchars", test_dispatch1<unsigned char>),
250+
Case("Testing callbacks with 0 uchars", test_dispatch0<unsigned char>),
251+
252+
Case("Testing callbacks with 5 uint64s", test_dispatch5<uint64_t>),
253+
Case("Testing callbacks with 4 uint64s", test_dispatch4<uint64_t>),
254+
Case("Testing callbacks with 3 uint64s", test_dispatch3<uint64_t>),
255+
Case("Testing callbacks with 2 uint64s", test_dispatch2<uint64_t>),
256+
Case("Testing callbacks with 1 uint64s", test_dispatch1<uint64_t>),
257+
Case("Testing callbacks with 0 uint64s", test_dispatch0<uint64_t>),
258+
259+
Case("Testing FunctionPointerArg1 compatibility", test_fparg1<int>),
260+
Case("Testing FunctionPointer compatibility", test_fparg0<int>),
261+
};
262+
263+
Specification specification(test_setup, cases);
264+
265+
int main() {
266+
return !Harness::run(specification);
267+
}

0 commit comments

Comments
 (0)