blob: d7a435db56f9f32f416be8e60a3c2719c61408fd [file] [log] [blame]
George Gekov23c26272021-08-16 11:32:10 +01001//
2// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include <catch.hpp>
7#include <limits>
8
9#include "MathUtils.hpp"
10#include <iostream>
11#include <numeric>
12
13TEST_CASE("Test DotProductF32")
14{
15 // Test Constants:
16 const int length = 6;
17
18 float inputA[] = { 1, 1, 1, 0, 0, 0 };
19 float inputB[] = { 0, 0, 0, 1, 1, 1 };
20
21 float dot_prod = MathUtils::DotProductF32(inputA, inputB, length);
22 float expectedResult = 0;
23 CHECK(dot_prod == expectedResult);
24}
25
26TEST_CASE("Test FFT32")
27{
28 // Test Constants:
29 std::vector<float> input(32, 0);
30 std::vector<float> output(32);
31 std::vector<float> expectedResult(32, 0);
32
33 MathUtils::FftF32(input, output);
34
35 // To avoid common failed assertions due to rounding of near-zero values a small offset is added
36 transform(output.begin(), output.end(), output.begin(),
37 bind2nd(std::plus<double>(), 0.1));
38
39 transform(expectedResult.begin(), expectedResult.end(), expectedResult.begin(),
40 bind2nd(std::plus<double>(), 0.1));
41
42 for (int i = 0; i < output.size(); i++)
43 {
44 CHECK (expectedResult[i] == Approx(output[i]));
45 }
46}
47
48TEST_CASE("Test ComplexMagnitudeSquaredF32")
49{
50 // Test Constants:
51 float input[] = { 0.0, 0.0, 0.5, 0.5,1,1 };
52 int inputLen = (sizeof(input)/sizeof(*input));
53 float expectedResult[] = { 0.0, 0.5, 2 };
54 int outputLen = inputLen/2;
55 float output[outputLen];
56
57 MathUtils::ComplexMagnitudeSquaredF32(input, inputLen, output, outputLen);
58
59 for (int i = 0; i < outputLen; i++)
60 {
61 CHECK (expectedResult[i] == Approx(output[i]));
62 }
63}
64
65TEST_CASE("Test VecLogarithmF32")
66{
67 // Test Constants:
68
69 std::vector<float> input = { 1, 0.1e-10 };
70 std::vector<float> expectedResult = { 0, -25.328436 };
71 std::vector<float> output(input.size());
72 MathUtils::VecLogarithmF32(input,output);
73
74 for (int i = 0; i < input.size(); i++)
75 {
76 CHECK (expectedResult[i] == Approx(output[i]));
77 }
78}
79
80TEST_CASE("Test MeanF32")
81{
82 // Test Constants:
83 float input[] = { 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 1.000 };
84 uint32_t inputLen = (sizeof(input)/sizeof(*input));
85 float output;
86
87 // Manually calculated mean of above array
88 float expectedResult = 0.100;
89 output = MathUtils::MeanF32(input, inputLen);
90
91 CHECK (expectedResult == Approx(output));
92}
93
94TEST_CASE("Test StdDevF32")
95{
96 // Test Constants:
97
98 float input[] = { 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 1.000 };
99
100 uint32_t inputLen = (sizeof(input)/sizeof(*input));
101
102 // Calculate mean using std library to avoid dependency on MathUtils::MeanF32
103 float mean = (std::accumulate(input, input + inputLen, 0.0f))/float(inputLen);
104
105 float output = MathUtils::StdDevF32(input, inputLen, mean);
106
107 // Manually calculated standard deviation of above array
108 float expectedResult = 0.300;
109
110 CHECK (expectedResult == Approx(output));
111}
112