1 /*
2  *  Copyright (C) 2019 Savoir-faire Linux Inc.
3  *
4  *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "valuetester.h"
21 
22 #include <iostream>
23 #include <string>
24 
25 // opendht
26 #include "opendht/value.h"
27 
28 namespace test {
29 CPPUNIT_TEST_SUITE_REGISTRATION(ValueTester);
30 
31 void
setUp()32 ValueTester::setUp() {
33 
34 }
35 
36 void
testConstructors()37 ValueTester::testConstructors() {
38     std::string the_data {"42 cats"};
39     dht::Value the_dht_value {(const uint8_t*)the_data.data(), the_data.size()};
40     std::string from_value {the_dht_value.data.begin(), the_dht_value.data.end()};
41     CPPUNIT_ASSERT_EQUAL(the_data, from_value);
42 }
43 
44 void
testFilter()45 ValueTester::testFilter()
46 {
47     dht::Value::Filter defaultFiler {};
48 
49     auto isPairSize = dht::Value::Filter([](const dht::Value& v) {
50         return v.data.size() % 2 == 0;
51     });
52 
53     auto isUserTypeTest = dht::Value::Filter([](const dht::Value& v) {
54         return v.user_type == "test";
55     });
56 
57     std::string data1 {"42 cats"};
58     dht::Value value1 {(const uint8_t*)data1.data(), data1.size()};
59     value1.user_type = "test";
60 
61     std::string data2 {"420 cats"};
62     dht::Value value2 {(const uint8_t*)data2.data(), data2.size()};
63     dht::Value value3 {(const uint8_t*)data2.data(), data2.size()};
64     value3.user_type = "test";
65 
66     CPPUNIT_ASSERT(!isPairSize(value1));
67     CPPUNIT_ASSERT(isUserTypeTest(value1));
68 
69     auto isBoth = dht::Value::Filter::chain(isPairSize, isUserTypeTest);
70     auto isUserTypeTest2 = dht::Value::Filter::chain(defaultFiler, isUserTypeTest);
71 
72     CPPUNIT_ASSERT(isUserTypeTest2(value1));
73     CPPUNIT_ASSERT(!isUserTypeTest2(value2));
74     CPPUNIT_ASSERT(!isBoth(value1));
75     CPPUNIT_ASSERT(!isBoth(value2));
76     CPPUNIT_ASSERT(isBoth(value3));
77 }
78 
79 void
tearDown()80 ValueTester::tearDown() {
81 
82 }
83 }  // namespace test
84