File: valuetester.cpp

package info (click to toggle)
opendht 3.0.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,284 kB
  • sloc: cpp: 23,342; python: 2,189; ansic: 2,041; makefile: 207; sh: 72
file content (83 lines) | stat: -rw-r--r-- 2,427 bytes parent folder | download
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
/*
 *  Copyright (C) 2014-2023 Savoir-faire Linux Inc.
 *
 *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include "valuetester.h"

#include <iostream>
#include <string>

// opendht
#include "opendht/value.h"

namespace test {
CPPUNIT_TEST_SUITE_REGISTRATION(ValueTester);

void
ValueTester::setUp() {

}

void
ValueTester::testConstructors() {
    std::string the_data {"42 cats"};
    dht::Value the_dht_value {(const uint8_t*)the_data.data(), the_data.size()};
    std::string from_value {the_dht_value.data.begin(), the_dht_value.data.end()};
    CPPUNIT_ASSERT_EQUAL(the_data, from_value);
}

void
ValueTester::testFilter()
{
    dht::Value::Filter defaultFiler {};

    auto isPairSize = dht::Value::Filter([](const dht::Value& v) {
        return v.data.size() % 2 == 0;
    });

    auto isUserTypeTest = dht::Value::Filter([](const dht::Value& v) {
        return v.user_type == "test";
    });

    std::string data1 {"42 cats"};
    dht::Value value1 {(const uint8_t*)data1.data(), data1.size()};
    value1.user_type = "test";

    std::string data2 {"420 cats"};
    dht::Value value2 {(const uint8_t*)data2.data(), data2.size()};
    dht::Value value3 {(const uint8_t*)data2.data(), data2.size()};
    value3.user_type = "test";

    CPPUNIT_ASSERT(!isPairSize(value1));
    CPPUNIT_ASSERT(isUserTypeTest(value1));

    auto isBoth = dht::Value::Filter::chain(isPairSize, isUserTypeTest);
    auto isUserTypeTest2 = dht::Value::Filter::chain(defaultFiler, isUserTypeTest);

    CPPUNIT_ASSERT(isUserTypeTest2(value1));
    CPPUNIT_ASSERT(!isUserTypeTest2(value2));
    CPPUNIT_ASSERT(!isBoth(value1));
    CPPUNIT_ASSERT(!isBoth(value2));
    CPPUNIT_ASSERT(isBoth(value3));
}

void
ValueTester::tearDown() {

}
}  // namespace test