FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
assertions.h
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16#ifndef TESTS_ASSERTIONS_H
17#define TESTS_ASSERTIONS_H
18
19#include "../flash.h"
20#include "../iomanip.h"
21#include "../streams.h"
22
23#define ASSERT(OUT, CONDITION) tests::assert_true(OUT, F("" #CONDITION ""), CONDITION)
24
25namespace tests
26{
27 void assert_true(streams::ostream& out, const char* message, bool condition)
28 {
29 if (!condition)
30 out << F("ASSERTION FAILED: ") << message << streams::endl;
31 }
32
33 void assert_true(streams::ostream& out, const flash::FlashStorage* message, bool condition)
34 {
35 if (!condition)
36 out << F("ASSERTION FAILED: ") << message << streams::endl;
37 }
38
39 template<typename T1, typename T2>
40 void assert_equals(streams::ostream& out, const char* var, T1 expected, T2 actual)
41 {
42 if (expected != actual)
43 out << F("ASSERTION FAILED on ") << var
44 << F(": expected = ") << expected << F(", actual=") << actual << streams::endl;
45 }
46
47 template<typename T1, typename T2>
48 void assert_equals(streams::ostream& out, const flash::FlashStorage* var, T1 expected, T2 actual)
49 {
50 if (expected != actual)
51 out << F("ASSERTION FAILED on ") << var
52 << F(": expected = ") << expected << F(", actual=") << actual << streams::endl;
53 }
54}
55#endif /* TESTS_ASSERTIONS_H */
Output stream wrapper to provide formatted output API, a la C++.
Definition: streams.h:61
#define F(ptr)
Force string constant to be stored as flash storage.
Definition: flash.h:150
void endl(FSTREAM &stream)
Manipulator for an output stream, which will insert a new-line character and flush the stream buffer.
Definition: streams.h:725