File: test_algorithm.cc

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (157 lines) | stat: -rw-r--r-- 4,485 bytes parent folder | download | duplicates (6)
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
/**
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2001-2014. ALL RIGHTS RESERVED.
* Copyright (C) UT-Battelle, LLC. 2014. ALL RIGHTS RESERVED.
* See file LICENSE for terms.
*/

#include <common/test.h>
extern "C" {
#include <ucs/algorithm/crc.h>
#include <ucs/algorithm/qsort_r.h>
#include <ucs/algorithm/string_distance.h>
}
#include <vector>

class test_algorithm : public ucs::test {
protected:

    static int compare_func(const void *elem1, const void *elem2)
    {
        return *(const int*)elem1 - *(const int*)elem2;
    }

    static int compare_func_r(const void *elem1, const void *elem2, void *arg)
    {
        EXPECT_TRUE(MAGIC == arg);
        return compare_func(elem1, elem2);
    }

    static void *MAGIC;
};

void *test_algorithm::MAGIC = (void*)0xdeadbeef1ee7a880ull;

UCS_TEST_F(test_algorithm, qsort_r) {
    for (int i = 0; i < 1000 / ucs::test_time_multiplier(); ++i) {
        unsigned nmemb = ucs::rand() % 100;

        std::vector<int> vec;
        for (unsigned j = 0; j < nmemb; ++j) {
            vec.push_back(ucs::rand() % 200);
        }

        std::vector<int> vec2 = vec;
        qsort(&vec2[0], nmemb, sizeof(int), compare_func);

        ucs_qsort_r(&vec[0], nmemb, sizeof(int), compare_func_r, MAGIC);
        ASSERT_EQ(vec2, vec);
    }
}

UCS_TEST_F(test_algorithm, crc16) {
    std::string test_str;

    test_str = "";
    EXPECT_EQ(0u, ucs_crc16_string(test_str.c_str()));

    test_str = "0";
    EXPECT_EQ(0xc1fbu, ucs_crc16_string(test_str.c_str()));

    test_str = "01";
    EXPECT_EQ(0x99efu, ucs_crc16_string(test_str.c_str()));

    test_str = "012";
    EXPECT_EQ(0xfd89u, ucs_crc16_string(test_str.c_str()));

    test_str = "0123";
    EXPECT_EQ(0xea54u, ucs_crc16_string(test_str.c_str()));

    test_str = "01234";
    EXPECT_EQ(0x9394u, ucs_crc16_string(test_str.c_str()));

    test_str = "012345";
    EXPECT_EQ(0x4468u, ucs_crc16_string(test_str.c_str()));

    test_str = "0123456";
    EXPECT_EQ(0x4bc7u, ucs_crc16_string(test_str.c_str()));

    test_str = "01234567";
    EXPECT_EQ(0x07bcu, ucs_crc16_string(test_str.c_str()));

    test_str = "012345678";
    EXPECT_EQ(0x3253u, ucs_crc16_string(test_str.c_str()));

    test_str = "0123456789";
    EXPECT_EQ(0x3c16u, ucs_crc16_string(test_str.c_str()));
}

UCS_TEST_F(test_algorithm, crc32) {
    std::string test_str;

    test_str = "";
    EXPECT_EQ(0u, ucs_crc32(0, test_str.c_str(), test_str.size()));

    test_str = "0";
    EXPECT_EQ(0xf4dbdf21ul, ucs_crc32(0, test_str.c_str(), test_str.size()));

    test_str = "01";
    EXPECT_EQ(0xcf412436ul, ucs_crc32(0, test_str.c_str(), test_str.size()));

    test_str = "012";
    EXPECT_EQ(0xd5a06ab0ul, ucs_crc32(0, test_str.c_str(), test_str.size()));

    test_str = "0123";
    EXPECT_EQ(0xa6669d7dul, ucs_crc32(0, test_str.c_str(), test_str.size()));

    test_str = "01234";
    EXPECT_EQ(0xdda47024ul, ucs_crc32(0, test_str.c_str(), test_str.size()));

    test_str = "012345";
    EXPECT_EQ(0xb86f6b0ful, ucs_crc32(0, test_str.c_str(), test_str.size()));

    test_str = "0123456";
    EXPECT_EQ(0x8dbf08eeul, ucs_crc32(0, test_str.c_str(), test_str.size()));

    test_str = "01234567";
    EXPECT_EQ(0x2d803af5ul, ucs_crc32(0, test_str.c_str(), test_str.size()));

    test_str = "012345678";
    EXPECT_EQ(0x37fad1baul, ucs_crc32(0, test_str.c_str(), test_str.size()));

    test_str = "0123456789";
    EXPECT_EQ(0xa684c7c6ul, ucs_crc32(0, test_str.c_str(), test_str.size()));
}

UCS_TEST_F(test_algorithm, string_distance) {
    // Empty strings
    EXPECT_EQ(0u, ucs_string_distance("", ""));

    // First string empty
    EXPECT_EQ(8u, ucs_string_distance("", "aabbccdd"));

    // Second string empty
    EXPECT_EQ(8u, ucs_string_distance("aabbccdd", ""));

    // 2 identical strings
    EXPECT_EQ(0u, ucs_string_distance("aabbccdd", "aabbccdd"));

    // No common chars
    EXPECT_EQ(8u, ucs_string_distance("aabbccdd", "eeffgghh"));

    // Replace
    EXPECT_EQ(2u, ucs_string_distance("aabbccdd", "aabeccfd"));

    // Swap
    EXPECT_EQ(2u, ucs_string_distance("aabbccdd", "aabcbcdd"));

    // Subtract
    EXPECT_EQ(2u, ucs_string_distance("aabbccdd", "aabcdd"));

    // Add
    EXPECT_EQ(2u, ucs_string_distance("aabbccdd", "aabbeeccdd"));

    // Combinations
    EXPECT_EQ(4u, ucs_string_distance("aabbccddee", "afbccdede"));
    EXPECT_EQ(4u, ucs_string_distance("aabbccddeeff", "ababccdgedeff"));
    EXPECT_EQ(6u, ucs_string_distance("aabbccddeeff", "aagbbhddefefii"));
}