Serenity Operating System
at hosted 134 lines 4.1 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 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 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/TestSuite.h> 28 29#include <AK/String.h> 30#include <AK/HashMap.h> 31 32TEST_CASE(construct) 33{ 34 typedef HashMap<int, int> IntIntMap; 35 EXPECT(IntIntMap().is_empty()); 36 EXPECT_EQ(IntIntMap().size(), 0u); 37} 38 39TEST_CASE(populate) 40{ 41 HashMap<int, String> number_to_string; 42 number_to_string.set(1, "One"); 43 number_to_string.set(2, "Two"); 44 number_to_string.set(3, "Three"); 45 46 EXPECT_EQ(number_to_string.is_empty(), false); 47 EXPECT_EQ(number_to_string.size(), 3u); 48} 49 50TEST_CASE(range_loop) 51{ 52 HashMap<int, String> number_to_string; 53 number_to_string.set(1, "One"); 54 number_to_string.set(2, "Two"); 55 number_to_string.set(3, "Three"); 56 57 int loop_counter = 0; 58 for (auto& it : number_to_string) { 59 EXPECT_EQ(it.value.is_null(), false); 60 ++loop_counter; 61 } 62 EXPECT_EQ(loop_counter, 3); 63} 64 65TEST_CASE(map_remove) 66{ 67 HashMap<int, String> number_to_string; 68 number_to_string.set(1, "One"); 69 number_to_string.set(2, "Two"); 70 number_to_string.set(3, "Three"); 71 72 number_to_string.remove(1); 73 EXPECT_EQ(number_to_string.size(), 2u); 74 EXPECT(number_to_string.find(1) == number_to_string.end()); 75 76 number_to_string.remove(3); 77 EXPECT_EQ(number_to_string.size(), 1u); 78 EXPECT(number_to_string.find(3) == number_to_string.end()); 79 EXPECT(number_to_string.find(2) != number_to_string.end()); 80} 81 82TEST_CASE(case_insensitive) 83{ 84 HashMap<String, int, CaseInsensitiveStringTraits> casemap; 85 EXPECT_EQ(String("nickserv").to_lowercase(), String("NickServ").to_lowercase()); 86 casemap.set("nickserv", 3); 87 casemap.set("NickServ", 3); 88 EXPECT_EQ(casemap.size(), 1u); 89} 90 91TEST_CASE(assert_on_iteration_during_clear) 92{ 93 struct Object { 94 ~Object() 95 { 96 m_map->begin(); 97 } 98 HashMap<int, Object>* m_map; 99 }; 100 HashMap<int, Object> map; 101 map.set(0, { &map }); 102 map.clear(); 103} 104 105TEST_CASE(hashmap_of_nonnullownptr_get) 106{ 107 struct Object { 108 Object(const String& s) : string(s) {} 109 String string; 110 }; 111 112 HashMap<int, NonnullOwnPtr<Object>> objects; 113 objects.set(1, make<Object>("One")); 114 objects.set(2, make<Object>("Two")); 115 objects.set(3, make<Object>("Three")); 116 117 { 118 auto x = objects.get(2); 119 EXPECT_EQ(x.has_value(), true); 120 EXPECT_EQ(x.value()->string, "Two"); 121 } 122 123 { 124 // Do it again to make sure that peeking into the map above didn't 125 // remove the value from the map. 126 auto x = objects.get(2); 127 EXPECT_EQ(x.has_value(), true); 128 EXPECT_EQ(x.value()->string, "Two"); 129 } 130 131 EXPECT_EQ(objects.size(), 3u); 132} 133 134TEST_MAIN(HashMap)