df

Dependencies:   mbed

Fork of APP1 by Team APP

Committer:
dupm2216
Date:
Wed Jan 18 02:38:05 2017 +0000
Revision:
21:a111be2582be
Parent:
18:a21199781d20
Add code header

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dupm2216 21:a111be2582be 1 /////////////////////////////////////////////////////////////
dupm2216 21:a111be2582be 2 // APP 1: Systèmes à microprocesseurs //
dupm2216 21:a111be2582be 3 // //
dupm2216 21:a111be2582be 4 // Université de Sherbrooke //
dupm2216 21:a111be2582be 5 // Génie informatique //
dupm2216 21:a111be2582be 6 // Session 5, Hiver 2017 //
dupm2216 21:a111be2582be 7 // //
dupm2216 21:a111be2582be 8 // Date: 17 janvier 2017 //
dupm2216 21:a111be2582be 9 // //
dupm2216 21:a111be2582be 10 // Auteurs: Maxime Dupuis, dupm2216 //
dupm2216 21:a111be2582be 11 // Bruno Allaire-Lemay, allb2701 //
dupm2216 21:a111be2582be 12 /////////////////////////////////////////////////////////////
dupm2216 21:a111be2582be 13
dupm2216 6:3facf0329142 14 #include "TestUtility.hpp"
dupm2216 6:3facf0329142 15 #include "Utility.hpp"
dupm2216 6:3facf0329142 16
dupm2216 6:3facf0329142 17 #include <cassert>
dupm2216 6:3facf0329142 18
dupm2216 6:3facf0329142 19 namespace utility
dupm2216 6:3facf0329142 20 {
dupm2216 6:3facf0329142 21 void run_all_tests()
dupm2216 6:3facf0329142 22 {
dupm2216 6:3facf0329142 23 test_is_almost_equal();
dupm2216 6:3facf0329142 24 test_degree_from_radian();
dupm2216 6:3facf0329142 25 test_wrap_angle();
GaiSensei 12:1c341b119b23 26 test_update_bit();
GaiSensei 12:1c341b119b23 27 test_update_bit_for_values_greater_than_one_byte();
GaiSensei 13:bb9669053eb3 28 test_update_bits();
dupm2216 18:a21199781d20 29 test_moving_average_filter();
dupm2216 6:3facf0329142 30 }
dupm2216 6:3facf0329142 31
dupm2216 6:3facf0329142 32 void test_is_almost_equal()
dupm2216 6:3facf0329142 33 {
dupm2216 6:3facf0329142 34 assert(is_almost_equal(0, 0, 0.01));
dupm2216 6:3facf0329142 35 assert(is_almost_equal(0.50, 0.51, 0.02));
dupm2216 6:3facf0329142 36 assert(!is_almost_equal(0.50, 0.52, 0.01));
dupm2216 6:3facf0329142 37 assert(!is_almost_equal(0, 0.50, 0.1));
dupm2216 6:3facf0329142 38 }
dupm2216 6:3facf0329142 39
dupm2216 6:3facf0329142 40 void test_degree_from_radian()
dupm2216 6:3facf0329142 41 {
dupm2216 6:3facf0329142 42 const double tolerance = 0.05;
dupm2216 6:3facf0329142 43 assert(is_almost_equal(0, degree_from_radian(0.0), tolerance));
dupm2216 6:3facf0329142 44 assert(is_almost_equal(90, degree_from_radian(PI/2.0), tolerance));
dupm2216 6:3facf0329142 45 assert(is_almost_equal(180, degree_from_radian(PI), tolerance));
dupm2216 6:3facf0329142 46 assert(is_almost_equal(270, degree_from_radian(3.0*PI/2.0), tolerance));
dupm2216 6:3facf0329142 47 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 48 assert(is_almost_equal(270, degree_from_radian(-PI/2.0), tolerance));
dupm2216 6:3facf0329142 49 }
dupm2216 6:3facf0329142 50
dupm2216 6:3facf0329142 51 void test_wrap_angle()
dupm2216 6:3facf0329142 52 {
dupm2216 6:3facf0329142 53 const double tolerance = 0.05;
dupm2216 6:3facf0329142 54 assert(is_almost_equal(0, wrap_angle(0.0), tolerance));
dupm2216 6:3facf0329142 55 assert(is_almost_equal(90, wrap_angle(90), tolerance));
dupm2216 6:3facf0329142 56 assert(is_almost_equal(180, wrap_angle(180), tolerance));
dupm2216 6:3facf0329142 57 assert(is_almost_equal(0, wrap_angle(360), tolerance) || is_almost_equal(360, wrap_angle(360), tolerance));
dupm2216 6:3facf0329142 58 assert(is_almost_equal(90, wrap_angle(360+90), tolerance));
dupm2216 6:3facf0329142 59 assert(is_almost_equal(270, wrap_angle(360-90), tolerance));
dupm2216 6:3facf0329142 60 }
GaiSensei 12:1c341b119b23 61
GaiSensei 12:1c341b119b23 62 void test_update_bit()
GaiSensei 12:1c341b119b23 63 {
GaiSensei 12:1c341b119b23 64 assert(4 == sizeof(int));
GaiSensei 12:1c341b119b23 65 assert(0x00 == update_bit(0x00, 0, 0));
GaiSensei 12:1c341b119b23 66 assert(0x01 == update_bit(0x00, 0, 1));
GaiSensei 12:1c341b119b23 67 assert(0x00 == update_bit(0x01, 0, 0));
GaiSensei 12:1c341b119b23 68 assert(0x05 == update_bit(0x07, 1, 0));
GaiSensei 12:1c341b119b23 69 }
GaiSensei 12:1c341b119b23 70
GaiSensei 12:1c341b119b23 71 void test_update_bit_for_values_greater_than_one_byte()
GaiSensei 12:1c341b119b23 72 {
GaiSensei 12:1c341b119b23 73 assert(0x7FFF == update_bit(0xFFFF, 15, 0));
GaiSensei 12:1c341b119b23 74 assert(0x7FFFFFFF == update_bit(0xFFFFFFFF, 31, 0));
GaiSensei 12:1c341b119b23 75 }
GaiSensei 13:bb9669053eb3 76
GaiSensei 13:bb9669053eb3 77 void test_update_bits()
GaiSensei 13:bb9669053eb3 78 {
dupm2216 15:b38d9d210e32 79 assert(0x0F == update_bits(0x00, 0, 3, 0xFFFFFFFF, 0x0F));
dupm2216 15:b38d9d210e32 80 assert(0x1E == update_bits(0x00, 1, 4, 0xFFFFFFFF, 0x0F));
dupm2216 15:b38d9d210e32 81 assert(0x00 == update_bits(0x00, 0, 3, 0x00000000, 0x0F));
dupm2216 15:b38d9d210e32 82 assert(0x00 == update_bits(0x00, 1, 4, 0x00000000, 0x0F));
GaiSensei 13:bb9669053eb3 83 }
dupm2216 18:a21199781d20 84
dupm2216 18:a21199781d20 85 void test_moving_average_filter()
dupm2216 18:a21199781d20 86 {
dupm2216 18:a21199781d20 87 MovingAverageFilter filter(5);
dupm2216 18:a21199781d20 88 assert(1 == filter.calculate(5));
dupm2216 18:a21199781d20 89 assert(2 == filter.calculate(5));
dupm2216 18:a21199781d20 90 assert(3 == filter.calculate(5));
dupm2216 18:a21199781d20 91 assert(4 == filter.calculate(5));
dupm2216 18:a21199781d20 92 assert(5 == filter.calculate(5));
dupm2216 18:a21199781d20 93 }
dupm2216 9:12519f9dd3cd 94 }