Bruno Allaire-Lemay
/
APP1test
df
Fork of APP1 by
TestUtility.cpp@12:1c341b119b23, 2017-01-15 (annotated)
- Committer:
- GaiSensei
- Date:
- Sun Jan 15 19:46:10 2017 +0000
- Revision:
- 12:1c341b119b23
- Parent:
- 9:12519f9dd3cd
- Child:
- 13:bb9669053eb3
Add utility function update_bit; ; Calculate new 4 byte value after changing a single bit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dupm2216 | 6:3facf0329142 | 1 | #include "TestUtility.hpp" |
dupm2216 | 6:3facf0329142 | 2 | #include "Utility.hpp" |
dupm2216 | 6:3facf0329142 | 3 | |
dupm2216 | 6:3facf0329142 | 4 | #include <cassert> |
dupm2216 | 6:3facf0329142 | 5 | |
dupm2216 | 6:3facf0329142 | 6 | namespace utility |
dupm2216 | 6:3facf0329142 | 7 | { |
dupm2216 | 6:3facf0329142 | 8 | void run_all_tests() |
dupm2216 | 6:3facf0329142 | 9 | { |
dupm2216 | 6:3facf0329142 | 10 | test_is_almost_equal(); |
dupm2216 | 6:3facf0329142 | 11 | test_degree_from_radian(); |
dupm2216 | 6:3facf0329142 | 12 | test_wrap_angle(); |
GaiSensei | 12:1c341b119b23 | 13 | test_update_bit(); |
GaiSensei | 12:1c341b119b23 | 14 | test_update_bit_for_values_greater_than_one_byte(); |
dupm2216 | 6:3facf0329142 | 15 | } |
dupm2216 | 6:3facf0329142 | 16 | |
dupm2216 | 6:3facf0329142 | 17 | void test_is_almost_equal() |
dupm2216 | 6:3facf0329142 | 18 | { |
dupm2216 | 6:3facf0329142 | 19 | assert(is_almost_equal(0, 0, 0.01)); |
dupm2216 | 6:3facf0329142 | 20 | assert(is_almost_equal(0.50, 0.51, 0.02)); |
dupm2216 | 6:3facf0329142 | 21 | assert(!is_almost_equal(0.50, 0.52, 0.01)); |
dupm2216 | 6:3facf0329142 | 22 | assert(!is_almost_equal(0, 0.50, 0.1)); |
dupm2216 | 6:3facf0329142 | 23 | } |
dupm2216 | 6:3facf0329142 | 24 | |
dupm2216 | 6:3facf0329142 | 25 | void test_degree_from_radian() |
dupm2216 | 6:3facf0329142 | 26 | { |
dupm2216 | 6:3facf0329142 | 27 | const double tolerance = 0.05; |
dupm2216 | 6:3facf0329142 | 28 | assert(is_almost_equal(0, degree_from_radian(0.0), tolerance)); |
dupm2216 | 6:3facf0329142 | 29 | assert(is_almost_equal(90, degree_from_radian(PI/2.0), tolerance)); |
dupm2216 | 6:3facf0329142 | 30 | assert(is_almost_equal(180, degree_from_radian(PI), tolerance)); |
dupm2216 | 6:3facf0329142 | 31 | assert(is_almost_equal(270, degree_from_radian(3.0*PI/2.0), tolerance)); |
dupm2216 | 6:3facf0329142 | 32 | assert(is_almost_equal(0, degree_from_radian(2.0*PI), tolerance) || is_almost_equal(360, degree_from_radian(2.0*PI), tolerance)); |
dupm2216 | 6:3facf0329142 | 33 | assert(is_almost_equal(270, degree_from_radian(-PI/2.0), tolerance)); |
dupm2216 | 6:3facf0329142 | 34 | } |
dupm2216 | 6:3facf0329142 | 35 | |
dupm2216 | 6:3facf0329142 | 36 | void test_wrap_angle() |
dupm2216 | 6:3facf0329142 | 37 | { |
dupm2216 | 6:3facf0329142 | 38 | const double tolerance = 0.05; |
dupm2216 | 6:3facf0329142 | 39 | assert(is_almost_equal(0, wrap_angle(0.0), tolerance)); |
dupm2216 | 6:3facf0329142 | 40 | assert(is_almost_equal(90, wrap_angle(90), tolerance)); |
dupm2216 | 6:3facf0329142 | 41 | assert(is_almost_equal(180, wrap_angle(180), tolerance)); |
dupm2216 | 6:3facf0329142 | 42 | assert(is_almost_equal(0, wrap_angle(360), tolerance) || is_almost_equal(360, wrap_angle(360), tolerance)); |
dupm2216 | 6:3facf0329142 | 43 | assert(is_almost_equal(90, wrap_angle(360+90), tolerance)); |
dupm2216 | 6:3facf0329142 | 44 | assert(is_almost_equal(270, wrap_angle(360-90), tolerance)); |
dupm2216 | 6:3facf0329142 | 45 | } |
GaiSensei | 12:1c341b119b23 | 46 | |
GaiSensei | 12:1c341b119b23 | 47 | void test_update_bit() |
GaiSensei | 12:1c341b119b23 | 48 | { |
GaiSensei | 12:1c341b119b23 | 49 | assert(4 == sizeof(int)); |
GaiSensei | 12:1c341b119b23 | 50 | assert(0x00 == update_bit(0x00, 0, 0)); |
GaiSensei | 12:1c341b119b23 | 51 | assert(0x01 == update_bit(0x00, 0, 1)); |
GaiSensei | 12:1c341b119b23 | 52 | assert(0x00 == update_bit(0x01, 0, 0)); |
GaiSensei | 12:1c341b119b23 | 53 | assert(0x05 == update_bit(0x07, 1, 0)); |
GaiSensei | 12:1c341b119b23 | 54 | } |
GaiSensei | 12:1c341b119b23 | 55 | |
GaiSensei | 12:1c341b119b23 | 56 | void test_update_bit_for_values_greater_than_one_byte() |
GaiSensei | 12:1c341b119b23 | 57 | { |
GaiSensei | 12:1c341b119b23 | 58 | assert(0x7FFF == update_bit(0xFFFF, 15, 0)); |
GaiSensei | 12:1c341b119b23 | 59 | assert(0x7FFFFFFF == update_bit(0xFFFFFFFF, 31, 0)); |
GaiSensei | 12:1c341b119b23 | 60 | } |
dupm2216 | 9:12519f9dd3cd | 61 | } |