forked from jamesfowkes/cryptology-rgb-puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnonlinear.cpp
100 lines (82 loc) · 3.09 KB
/
nonlinear.cpp
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
#include <stdint.h>
#include <math.h>
void update_nonlinear_table(uint16_t * p_table, uint32_t maximum, uint8_t nsteps)
{
uint32_t sqrt_max;
uint32_t linear_step;
uint32_t linear;
uint32_t ngaps = nsteps - 1;
if (p_table)
{
sqrt_max = sqrt(maximum);
linear_step = (sqrt_max + (ngaps / 2)) / ngaps;
linear = 0;
for (uint8_t i = 0; i<ngaps; i++)
{
p_table[i] = linear * linear;
linear += linear_step;
}
p_table[ngaps] = maximum;
}
}
#ifdef UNIT_TEST
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
class BrightnessTableTestTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(BrightnessTableTestTest);
CPPUNIT_TEST(test_three_step_generation_with_maximum_of_4);
CPPUNIT_TEST(test_three_step_generation_with_maximum_of_5);
CPPUNIT_TEST(test_five_step_generation_with_maximum_of_4095);
CPPUNIT_TEST(test_eight_step_generation_with_maximum_of_4095);
CPPUNIT_TEST_SUITE_END();
void test_three_step_generation_with_maximum_of_4()
{
uint16_t test_table[3];
update_nonlinear_table(test_table, 4, 3);
CPPUNIT_ASSERT_EQUAL((uint16_t)0U, test_table[0]);
CPPUNIT_ASSERT_EQUAL((uint16_t)1U, test_table[1]);
CPPUNIT_ASSERT_EQUAL((uint16_t)4U, test_table[2]);
}
void test_three_step_generation_with_maximum_of_5()
{
uint16_t test_table[3];
update_nonlinear_table(test_table, 5, 3);
CPPUNIT_ASSERT_EQUAL((uint16_t)0U, test_table[0]);
CPPUNIT_ASSERT_EQUAL((uint16_t)1U, test_table[1]);
CPPUNIT_ASSERT_EQUAL((uint16_t)5U, test_table[2]);
}
void test_five_step_generation_with_maximum_of_4095()
{
uint16_t test_table[5];
update_nonlinear_table(test_table, 4095, 5);
CPPUNIT_ASSERT_EQUAL((uint16_t)0U, test_table[0]);
CPPUNIT_ASSERT_EQUAL((uint16_t)256U, test_table[1]);
CPPUNIT_ASSERT_EQUAL((uint16_t)1024U, test_table[2]);
CPPUNIT_ASSERT_EQUAL((uint16_t)2304U, test_table[3]);
CPPUNIT_ASSERT_EQUAL((uint16_t)4095U, test_table[4]);
}
void test_eight_step_generation_with_maximum_of_4095()
{
uint16_t test_table[8];
update_nonlinear_table(test_table, 4095, 8);
CPPUNIT_ASSERT_EQUAL((uint16_t)0U, test_table[0]);
CPPUNIT_ASSERT_EQUAL((uint16_t)81U, test_table[1]);
CPPUNIT_ASSERT_EQUAL((uint16_t)324U, test_table[2]);
CPPUNIT_ASSERT_EQUAL((uint16_t)729U, test_table[3]);
CPPUNIT_ASSERT_EQUAL((uint16_t)1296U, test_table[4]);
CPPUNIT_ASSERT_EQUAL((uint16_t)2025U, test_table[5]);
CPPUNIT_ASSERT_EQUAL((uint16_t)2916U, test_table[6]);
CPPUNIT_ASSERT_EQUAL((uint16_t)4095U, test_table[7]);
}
};
int main()
{
CppUnit::TextUi::TestRunner runner;
CPPUNIT_TEST_SUITE_REGISTRATION( BrightnessTableTestTest );
CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest( registry.makeTest() );
runner.run();
return 0;
}
#endif