1 /*
2  *  Copyright (c) 2020, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <string.h>
30 
31 #include "test_platform.h"
32 
33 #include <openthread/config.h>
34 
35 #include "common/instance.hpp"
36 #include "utils/lookup_table.hpp"
37 
38 #include "test_util.h"
39 
40 typedef ot::Utils::LookupTable::Entry Entry;
41 
42 struct TableEntryBase
43 {
TableEntryBaseTableEntryBase44     constexpr explicit TableEntryBase(uint8_t aValue)
45         : mValue(aValue)
46     {
47     }
48 
49     uint8_t mValue;
50 };
51 
52 struct TableEntry : public TableEntryBase, public Entry
53 {
TableEntryTableEntry54     constexpr TableEntry(const char *aName, uint8_t aValue)
55         : TableEntryBase(aValue)
56         , Entry(aName)
57         , mUint16(0xabba)
58         , mUint8(aValue)
59     {
60     }
61 
62     uint16_t mUint16;
63     uint8_t  mUint8;
64 };
65 
TestLookupTable(void)66 void TestLookupTable(void)
67 {
68     enum : uint16_t
69     {
70         kMaxNameSize = 30,
71     };
72 
73     constexpr TableEntry kTable[] = {
74         {"arkham city", 9}, {"arkham knight", 7}, {"bloodborne", 10}, {"god of war", 10},       {"horizon", 9},
75         {"infamous", 7},    {"last guardian", 7}, {"last of us", 11}, {"last of us part 2", 8}, {"mass effect", 8},
76         {"sekiro", 10},     {"tomb raider", 9},   {"uncharted", 9},
77     };
78 
79     constexpr Entry kUnsortedTable[]       = {Entry("z"), Entry("a"), Entry("b")};
80     constexpr Entry kDuplicateEntryTable[] = {Entry("duplicate"), Entry("duplicate")};
81 
82     static_assert(ot::Utils::LookupTable::IsSorted(kTable), "LookupTable::IsSorted() failed");
83     static_assert(!ot::Utils::LookupTable::IsSorted(kUnsortedTable), "failed for unsorted table");
84     static_assert(!ot::Utils::LookupTable::IsSorted(kDuplicateEntryTable), "failed for table with duplicate entries");
85 
86     for (const TableEntry &tableEntry : kTable)
87     {
88         const TableEntry *entry;
89         char              name[kMaxNameSize];
90 
91         strcpy(name, tableEntry.mName);
92 
93         entry = ot::Utils::LookupTable::Find(name, kTable);
94         VerifyOrQuit(entry == &tableEntry, "LookupTable::Find() failed");
95 
96         name[strlen(name) - 1] = '\0';
97         entry                  = ot::Utils::LookupTable::Find(name, kTable);
98         VerifyOrQuit(entry == nullptr, "LookupTable::Find() failed with non-matching name");
99     }
100 
101     VerifyOrQuit(ot::Utils::LookupTable::Find("dragon age", kTable) == nullptr, "failed with non-exiting match");
102 }
103 
main(void)104 int main(void)
105 {
106     TestLookupTable();
107     printf("All tests passed\n");
108     return 0;
109 }
110