Anklang 0.3.0-460-gc4ef46ba
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
sortnet.cc
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#include "sortnet.hh"
3#include "defs.hh"
4#include "internal.hh"
5
6// == Testing ==
7#include "testing.hh"
8
9namespace { // Anon
10using namespace Ase;
11
12template<size_t N, class T = int> void
13check_permutations ()
14{
15 std::array<T,N> permarray;
16 for (size_t i = 0; i < N; i++)
17 permarray[i] = i;
18 do
19 {
20 std::array<T,N> array { permarray };
21 fixed_sort (&array[0], &array[N], std::less<T>());
22 for (size_t i = 1; i < N; i++)
23 assert_return (array[i - 1] <= array[i]);
24 }
25 while (std::next_permutation (&permarray[0], &permarray[N]));
26}
27
28static uint64_t romu_state64a = 1, romu_state64b = 0xda942042e4dd58b5ULL;
29static inline uint64_t
30romumono2()
31{
32 const uint64_t result1 = romu_state64a, result2 = romu_state64b;
33 romu_state64a = rotl (romu_state64a, 32) * 15241094284759029579u;
34 romu_state64b = rotl (romu_state64b, 32) * 0x5851f42d4c957f2dUL;
35 return result1 | (uint64_t (result2) << 32);
36}
37
38template<size_t N, class T = int> void
39check_randomized (size_t runs)
40{
41 std::array<T,N> array;
42 for (size_t j = 0; j < runs; j++)
43 {
44 for (size_t i = 0; i < N; i++)
45 array[i] = romumono2();
46 fixed_sort (&array[0], &array[N], std::less<T>());
47 for (size_t i = 1; i < N; i++)
48 assert_return (array[i - 1] <= array[i]);
49 }
50}
51
52TEST_INTEGRITY (sortnet_tests);
53
54static void
55sortnet_tests()
56{
57 { // setup PRNG
58 romu_state64a = Test::random_int64() | 1;
59 romu_state64b = Test::random_int64();
60 romumono2(); romumono2(); romumono2(); romumono2(); romumono2();
61 }
62 constexpr const size_t RUNS = 9999;
63 check_permutations<1,int>();
64 check_permutations<2,int>();
65 check_permutations<3,int>();
66 check_permutations<4,int>();
67 check_permutations<5,int>();
68 check_permutations<6,int>();
69 check_permutations<7,int>();
70 check_permutations<8,int>();
71 check_permutations<9,int>();
72 check_randomized<10,int> (RUNS);
73 check_randomized<11,int> (RUNS);
74 check_randomized<12,int> (RUNS);
75 check_randomized<13,int> (RUNS);
76 check_randomized<14,int> (RUNS);
77 check_randomized<15,int> (RUNS);
78 check_randomized<16,int> (RUNS);
79
80 // sorted vector
81 SortedVector<int> s1 ({ 4,3,2,1, 9,8,7,6 });
82 TASSERT (s1.sorted ());
83 TASSERT (4 == *s1.find (4));
84 TASSERT (s1.end() == s1.find (5));
85 TASSERT (6 == *const_cast<const SortedVector<int>&> (s1).find (6));
86 TASSERT (false == s1.contains (5));
87 s1.insert (5);
88 TASSERT (5 == *s1.find (5));
89 TASSERT (true == s1.contains (5));
90 s1.data()[1] = 1;
91 TASSERT (s1.sorted () == false && s1.sorted (true) == true);
92 TASSERT (s1[0] == s1[1]);
93 const size_t col = s1.collapse();
94 TASSERT (col == 1 && s1[0] != s1[1]);
95 TASSERT (s1.collapse() == 0);
96 erase_if (s1, [] (auto v) { return v & 1; });
97 TASSERT (s1.size() == 3 && s1[0] == 4 && s1[1] == 6 && s1[2] == 8);
98 s1.clear();
99 TASSERT (s1.size() == 0 && s1.sorted());
100}
101
102} // Anon
Vector that keeps its elements sorted.
Definition sortnet.hh:223
#define assert_return(expr,...)
Return from the current function if expr is unmet and issue an assertion warning.
Definition internal.hh:29
#define TEST_INTEGRITY(FUNC)
Register func as an integrity test.
Definition internal.hh:77
uint64_t random_int64()
Return random int for reproduceble tests.
Definition testing.cc:185
The Anklang C++ API namespace.
Definition api.hh:9
void fixed_sort(RandomIt first, RandomIt last, Compare comp=std::less< typename std::iterator_traits< RandomIt >::value_type >())
Use sorting networks to sort containers <= 16 elements without allocations.
Definition sortnet.hh:193
T next_permutation(T... args)
T rotl(T... args)
typedef uint64_t
#define TASSERT(cond)
Unconditional test assertion, enters breakpoint if not fullfilled.
Definition testing.hh:24