PX4 Firmware
PX4 Autopilot Software http://px4.io
test_int.cpp
Go to the documentation of this file.
1
/****************************************************************************
2
*
3
* Copyright (C) 2012-2019 PX4 Development Team. All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
*
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in
13
* the documentation and/or other materials provided with the
14
* distribution.
15
* 3. Neither the name PX4 nor the names of its contributors may be
16
* used to endorse or promote products derived from this software
17
* without specific prior written permission.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
* POSSIBILITY OF SUCH DAMAGE.
31
*
32
****************************************************************************/
33
34
/**
35
* @file test_int.cpp
36
* Tests for integer types.
37
*/
38
39
#include <
unit_test.h
>
40
41
#include <errno.h>
42
#include <fcntl.h>
43
#include <float.h>
44
#include <math.h>
45
#include <px4_platform_common/px4_config.h>
46
#include <stdint.h>
47
#include <stdio.h>
48
#include <stdlib.h>
49
#include <sys/types.h>
50
#include <unistd.h>
51
52
typedef
union
{
53
int32_t
i
;
54
int64_t
l
;
55
uint8_t b[8];
56
}
test_32_64_t
;
57
58
class
IntTest
:
public
UnitTest
59
{
60
public
:
61
virtual
bool
run_tests();
62
63
private
:
64
bool
math64bitTests();
65
bool
math3264MixedMathTests();
66
};
67
68
bool
IntTest::math64bitTests
()
69
{
70
int64_t large = 354156329598;
71
int64_t calc = large * 5;
72
73
ut_assert
(
"354156329598 * 5 == 1770781647990"
, calc == 1770781647990);
74
75
return
true
;
76
}
77
78
bool
IntTest::math3264MixedMathTests
()
79
{
80
int32_t small = 50;
81
int32_t large_int = 2147483647;
// MAX INT value
82
83
uint64_t small_times_large = large_int * (uint64_t)small;
84
85
ut_assert
(
"64bit calculation: 50 * 2147483647 (max int val) == 107374182350"
, small_times_large == 107374182350);
86
87
return
true
;
88
}
89
90
91
bool
IntTest::run_tests
()
92
{
93
ut_run_test
(math64bitTests);
94
ut_run_test
(math3264MixedMathTests);
95
96
return
(_tests_failed == 0);
97
}
98
99
ut_declare_test_c
(
test_int
,
IntTest
)
test_32_64_t::l
int64_t l
Definition:
test_int.cpp:54
test_32_64_t::i
int32_t i
Definition:
test_int.cpp:53
ut_declare_test_c
#define ut_declare_test_c(test_function, test_class)
Definition:
unit_test.h:40
IntTest::math3264MixedMathTests
bool math3264MixedMathTests()
Definition:
test_int.cpp:78
IntTest::run_tests
virtual bool run_tests()
Override to run your unit tests.
Definition:
test_int.cpp:91
IntTest::math64bitTests
bool math64bitTests()
Definition:
test_int.cpp:68
UnitTest
Base class to be used for unit tests.
Definition:
unit_test.h:54
test_32_64_t
Definition:
test_int.cpp:52
ut_assert
#define ut_assert(message, test)
Used to assert a value within a unit test.
Definition:
unit_test.h:113
unit_test.h
IntTest
Definition:
test_int.cpp:58
ut_run_test
#define ut_run_test(test)
Runs a single unit test.
Definition:
unit_test.h:96
test_int
int test_int(int argc, char *argv[])
src
systemcmds
tests
test_int.cpp
Generated by
1.8.13