ModelSpace
All Classes Namespaces Functions Variables Enumerations Pages
clockwerktesttools.hpp
1/******************************************************************************
2* Copyright (c) ATTX LLC 2024. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, LLC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX, LLC.
15******************************************************************************/
16/*
17Clockwerk Test Tools File
18-------------------------
19This file defines tools for use in testing all code from the clockwerk module.
20It is support code for clockwerk_test.cpp.
21
22Author: Alex Reynolds
23*/
24
25#ifndef CLOCKWERK_TEST_TOOLS_HPP
26#define CLOCKWERK_TEST_TOOLS_HPP
27
28#include <cmath>
29
30#include "core/Matrix.hpp"
31
32namespace clockwerk {
33
34 // @brief Function to compare two matrices and return the result
35 // @param expected The expected matrix (defined by tester)
36 // @param actual The actual matrix (result of test)
37 // @return True if matrices match. False otherwise
38 // Note: Assumes matrix getters have been tested/are working
39 template<typename T, unsigned int R, unsigned int C>
40 bool matrixCompare(Matrix<T, R, C> expected, Matrix<T, R, C> actual, bool print=false) {
41 bool test_pass = true;
42
43 // Compare the size of the two matrices to ensure they match
44 std::pair<unsigned int, unsigned int> exp_size = expected.size();
45 std::pair<unsigned int, unsigned int> act_size = actual.size();
46 if(exp_size.first != act_size.first || exp_size.second != act_size.second) {
47 test_pass = false;
48 }
49
50 // Now loop through matrices and ensure they match
51 T expected_val;
52 T actual_val;
53 for(unsigned int i = 0; i < exp_size.first; i++) {
54 for(unsigned int j = 0; j < exp_size.second; j++) {
55 expected.get(i, j, expected_val);
56 actual.get(i, j, actual_val);
57 if(expected_val != actual_val) {
58 test_pass = false;
59 }
60 if(std::isnan(actual_val)) {
61 test_pass = false;
62 }
63 }
64 }
65
66 // Print if commanded or if test failed
67 if(print || !test_pass) {
68 std::cout<<"Expected:"<<std::endl;
69 expected.dump();
70 std::cout<<"Actual: "<<std::endl;
71 actual.dump();
72 }
73
74 return test_pass;
75 }
76
77 // @brief Function to compare two matrices and return the result
78 // @param expected The expected matrix (defined by tester)
79 // @param actual The actual matrix (result of test)
80 // @return True if matrices match. False otherwise
81 // Note: Assumes matrix getters have been tested/are working
82 template<typename T, unsigned int R, unsigned int C>
83 bool matrixCompareTol(Matrix<T, R, C> expected, Matrix<T, R, C> actual, const double &tolerance, bool print=false) {
84 bool test_pass = true;
85
86 // Compare the size of the two matrices to ensure they match
87 std::pair<unsigned int, unsigned int> exp_size = expected.size();
88 std::pair<unsigned int, unsigned int> act_size = actual.size();
89 if(exp_size.first != act_size.first || exp_size.second != act_size.second) {
90 test_pass = false;
91 }
92
93 // Now loop through matrices and ensure they match
94 T expected_val;
95 T actual_val;
96 for(unsigned int i = 0; i < exp_size.first; i++) {
97 for(unsigned int j = 0; j < exp_size.second; j++) {
98 expected.get(i, j, expected_val);
99 actual.get(i, j, actual_val);
100 if(std::abs(expected_val - actual_val) > tolerance) {
101 test_pass = false;
102 }
103 if(std::isnan(actual_val)) {
104 test_pass = false;
105 }
106 }
107 }
108
109 // Print if commanded or if test failed
110 if(print || !test_pass) {
111 std::cout<<"Expected:"<<std::endl;
112 expected.dump();
113 std::cout<<"Actual: "<<std::endl;
114 actual.dump();
115 }
116
117 return test_pass;
118 }
119
120 // @brief Function to compare two arrays and return the result
121 // @param expected The expected array (defined by tester)
122 // @param actual The actual array (result of test)
123 // @return True if arrays match. False otherwise
124 template<typename T, unsigned long int N>
125 bool arrayCompareTol(std::array<T, N> expected, std::array<T, N> actual, const double &tolerance, bool print=false) {
126 bool test_pass = true;
127
128 // Now loop through matrices and ensure they match
129 for(unsigned int i = 0; i < N; i++) {
130 if(std::abs(expected[i] - actual[i]) > tolerance) {
131 test_pass = false;
132 }
133 if(std::isnan(actual[i])) {
134 test_pass = false;
135 }
136 }
137
138 // Print if commanded or test failed
139 if(print || !test_pass) {
140 std::cout<<"Expected: [";
141 for(unsigned int i = 0; i < N; i++) {
142 std::cout<<expected[i];
143 if(i < N - 1) {
144 std::cout<<" ";
145 }
146 }
147 std::cout<<"]\nActual: [";
148 for(unsigned int i = 0; i < N; i++) {
149 std::cout<<actual[i];
150 if(i < N - 1) {
151 std::cout<<" ";
152 }
153 }
154 std::cout<<"]"<<std::endl;
155 }
156
157 return test_pass;
158 }
159
160}
161
162#endif
Matrix math implementation.
Definition Matrix.hpp:54