File: vec_test.cpp

package info (click to toggle)
supercollider-sc3-plugins 3.13.0~repack-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 20,104 kB
  • sloc: cpp: 303,352; lisp: 9,589; ansic: 3,547; sh: 96; makefile: 87; haskell: 21
file content (161 lines) | stat: -rw-r--r-- 3,725 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#define BOOST_TEST_MAIN
#include <boost/test/included/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>

#include <cmath>

#include "../vec.hpp"

using namespace nova;
using namespace std;

template <typename F>
void test_get_set(void)
{
    vec<F> v;
    for (int i = 0; i != v.size; ++i)
        v.set(i, (F)1.0*i);

    for (int i = 0; i != v.size; ++i)
        BOOST_REQUIRE_EQUAL( v.get(i), F(1.0*i));

    vec<F> w;
    w.set_slope(0, 1.0);
    for (int i = 0; i != v.size; ++i)
        BOOST_REQUIRE_EQUAL( w.get(i), F(1.0*i));
}

template <typename VecType>
void compare_float_mask(VecType const & v, unsigned int mask)
{
   for (int i = 0; i != v.size; ++i) {
        union {
            float f;
            unsigned int i;
        } x;
        x.f = v.get(i);
        BOOST_REQUIRE_EQUAL( x.i, mask);
    }
}

void test_gen_float(void)
{
    typedef vec<float> vec_t;
#if defined(__SSE__) || defined (__AVX__)
    vec_t zero = vec_t::gen_zero();
    for (int i = 0; i != zero.size; ++i)
        BOOST_REQUIRE_EQUAL( zero.get(i), 0.f);

    vec_t one = vec_t::gen_one();
    for (int i = 0; i != one.size; ++i)
        BOOST_REQUIRE_EQUAL( one.get(i), 1.f);

    vec_t half = vec_t::gen_05();
    for (int i = 0; i != one.size; ++i)
        BOOST_REQUIRE_EQUAL( half.get(i), 0.5f);

    vec_t sign_mask = vec_t::gen_sign_mask();
    compare_float_mask(sign_mask, 0x80000000);

    vec_t abs_mask = vec_t::gen_abs_mask();
    compare_float_mask(abs_mask, 0x7fffffff);

    vec_t exp_mask = vec_t::gen_exp_mask();
    compare_float_mask(exp_mask, 0x7F800000);

    vec_t exp1_mask = vec_t::gen_exp_mask_1();
    compare_float_mask(exp1_mask, 0x3F000000);
#endif
}

BOOST_AUTO_TEST_CASE( get_set )
{
    test_get_set<float>();
    test_get_set<double>();
    test_gen_float();
}

template <typename T>
void test_slope(void)
{
    typedef vec<T> vec_t;
    vec_t vec;
    T increment = vec.set_slope(0, 1);
    T expected_increment = vec_t::size * 1;
    BOOST_REQUIRE_CLOSE_FRACTION( increment, expected_increment, 0.001);

    for (int i = 0; i != vec_t::size; ++i)
        BOOST_REQUIRE_CLOSE_FRACTION( vec.get(i), i, 0.001);
}

BOOST_AUTO_TEST_CASE( slope )
{
    test_slope<float>();
    test_slope<double>();
}

template <typename T>
void test_select(void)
{
    typedef vec<T> vec_t;
    vec_t a(1.0), b(2.0);
    vec_t zero = vec_t::gen_zero();
    vec_t ones = vec_t::gen_ones();

    vec_t select_a = select(a, b, zero);
    vec_t select_b = select(a, b, ones);

    BOOST_REQUIRE(select_a.get(0) == 1);
    BOOST_REQUIRE(select_b.get(0) == 2);
}

BOOST_AUTO_TEST_CASE( select_tester )
{
    test_select<float>();
    test_select<double>();
}

BOOST_AUTO_TEST_CASE( align_ )
{
    BOOST_REQUIRE(vec<double>::is_aligned(NULL));
    BOOST_REQUIRE(vec<float>::is_aligned(NULL));
    BOOST_REQUIRE(!vec<double>::is_aligned((double*)(1)));
    BOOST_REQUIRE(!vec<float>::is_aligned((float*)(1)));
}

template <typename T>
void test_undenormalize(void)
{
    const T min_positive_value = std::numeric_limits<T>::min();
    typedef vec<T> vec_t;
    vec_t denormal(min_positive_value/2);

    vec_t fixed = undenormalize(denormal);
    BOOST_REQUIRE(fixed.get(0) == 0);

    vec_t normal(min_positive_value);
    BOOST_REQUIRE(normal.get(0) == min_positive_value);
}

BOOST_AUTO_TEST_CASE( undenormalize_tester )
{
    test_undenormalize<float>();
    test_undenormalize<double>();
}

template <typename T>
void test_reciprocal(void)
{
    typedef vec<T> vec_t;
    vec_t four(4.0);
    T result = reciprocal(four).get(0);
    BOOST_REQUIRE_CLOSE(result, 0.25, 1e-5);
}


BOOST_AUTO_TEST_CASE( reciprocal_tester )
{
    test_reciprocal<float>();
    test_reciprocal<double>();
}