Skip to content

Commit 6b36f6e

Browse files
committed
Move abs+round checks to test/device/test_sw
1 parent 555c272 commit 6b36f6e

File tree

2 files changed

+82
-30
lines changed

2 files changed

+82
-30
lines changed

libraries/esp8266/examples/math/math.ino

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Small math example, checking whether we properly integrate with c++ math
3+
- https://github.com/esp8266/Arduino/issues/5530
4+
- https://github.com/espressif/arduino-esp32/pull/2738
5+
6+
Released to public domain
7+
*/
8+
9+
#include <BSTest.h>
10+
#include <type_traits>
11+
12+
BS_ENV_DECLARE();
13+
14+
void setup()
15+
{
16+
Serial.begin(115200);
17+
BS_RUN(Serial);
18+
}
19+
20+
bool pretest()
21+
{
22+
return true;
23+
}
24+
25+
#define TEST_MATH_IS_SAME(OP1, OP2) \
26+
std::is_same<decltype(OP1), decltype(OP2)>::value
27+
28+
TEST_CASE("std::abs and abs result is the same", "[arduino-math]")
29+
{
30+
CHECK(TEST_MATH_IS_SAME(abs(-5), std::abs(-5)));
31+
CHECK(TEST_MATH_IS_SAME(abs(-25.0), std::abs(-25.0)));
32+
CHECK(TEST_MATH_IS_SAME(abs(10.0), std::abs(10.0)));
33+
CHECK(! TEST_MATH_IS_SAME(abs(10.0), std::abs(10)));
34+
CHECK(! TEST_MATH_IS_SAME(abs(-5), std::abs(10.0)));
35+
}
36+
37+
TEST_CASE("abs works with ints", "[arduino-math]")
38+
{
39+
int a = -3;
40+
int b = 3;
41+
CHECK(TEST_MATH_IS_SAME(abs(a), a));
42+
CHECK(TEST_MATH_IS_SAME(abs(b), b));
43+
CHECK(abs(a) == b);
44+
CHECK(abs(b) == b);
45+
}
46+
47+
bool compare_floats(float a, float b) {
48+
return std::fabs(a - b) < std::numeric_limits<float>::epsilon();
49+
}
50+
51+
TEST_CASE("abs works with floats", "[arduino-math]")
52+
{
53+
float a = -3.5;
54+
float b = 3.5;
55+
CHECK(TEST_MATH_IS_SAME(abs(a), a));
56+
CHECK(TEST_MATH_IS_SAME(abs(b), b));
57+
CHECK(compare_floats(abs(a), b));
58+
CHECK(compare_floats(abs(b), b));
59+
}
60+
61+
TEST_CASE("round works with floats", "[arduino-math]")
62+
{
63+
float a = 2.9;
64+
float b = 3.0;
65+
CHECK(TEST_MATH_IS_SAME(round(a), a));
66+
CHECK(TEST_MATH_IS_SAME(round(b), b));
67+
CHECK(compare_floats(round(a), b));
68+
CHECK(compare_floats(round(b), b));
69+
}
70+
71+
TEST_CASE("round result is float", "[arduino-math]")
72+
{
73+
float a = 2.9;
74+
float b = 3.0;
75+
CHECK(TEST_MATH_IS_SAME(round(a), a));
76+
CHECK(TEST_MATH_IS_SAME(round(b), b));
77+
CHECK(compare_floats(round(a), b));
78+
CHECK(compare_floats(round(b), b));
79+
}
80+
81+
void loop(){}
82+

0 commit comments

Comments
 (0)