Echo Writes Code

test_collections.cpp

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
#include "crucible/core.hpp"
#include "crucible/test.hpp"

namespace test_collections
{
	CRUCIBLE_TEST_SCENARIO(make_empty_hash_map)
	{
		auto hm { crucible::make_hash_map<int, int>() };
		CRUCIBLE_ASSERT_EQ(hm.entry_count(), 0);
	}

	CRUCIBLE_TEST_SCENARIO(make_populated_hash_map)
	{
		auto const phrases {
			crucible::make_hash_map<crucible::string, crucible::string>({
				{ "hello", "bonjour" }, { "goodbye", "au revoir" }, { "how's it going?", "ca va?" }
			})
		};

		CRUCIBLE_ASSERT_EQ(phrases.get("hello"), "bonjour");
		CRUCIBLE_ASSERT_EQ(phrases.get("goodbye"), "au revoir");
		CRUCIBLE_ASSERT_EQ(phrases.get("how's it going?"), "ca va?");
	}

	CRUCIBLE_TEST_SCENARIO(hash_map_put_get)
	{
		crucible::hash_map<crucible::string, std::size_t> pokemon;
		pokemon.put("bulbasaur", 1);
		CRUCIBLE_ASSERT_EQ(pokemon.get("bulbasaur"), 1);
	}

	CRUCIBLE_TEST_SCENARIO(hash_map_small_load)
	{
		auto const pokemon_names {
			crucible::make_heap_buffer<crucible::string_view>({
				"bulbasaur", "ivysaur", "venusaur", "charmander", "charmeleon", "charizard", "squirtle",
				"wartortle", "blastoise", "caterpie", "metapod", "butterfree", "weedle", "kakuna",
				"beedrill", "pidgey", "pidgeotto", "pidgeot", "rattata", "raticate", "spearow", "fearow",
				"ekans", "arbok", "pikachu", "raichu", "sandshrew", "sandslash", "nidoran f", "nidorina",
				"nidoqueen", "nidoran m", "nidorino", "nidoking", "clefairy", "clefable", "vulpix",
				"ninetales", "jigglypuff", "wigglytuff", "zubat", "golbat", "oddish", "gloom", "vileplume",
				"paras", "parasect", "venonat", "venomoth", "diglett", "dugtrio", "meowth", "persian",
				"psyduck", "golduck", "mankey", "primeape", "growlithe", "arcanine", "poliwag", "poliwhirl",
				"poliwrath", "abra", "kadabra", "alakazam", "machop", "machoke", "machamp", "bellsprout",
				"weepinbell", "victreebell", "tentacool", "tentacruel", "geodude", "graveler", "golem",
				"ponyta", "rapidash", "slowpoke", "slowbro", "magnemite", "magneton", "farfetch'd", "doduo",
				"dodrio", "seel", "dewgong", "grimer", "muk", "shellder", "cloyster", "gastly", "haunter",
				"gengar", "onix", "drowzee", "hypno", "krabby", "kingler", "voltorb", "electrode",
				"exeggcute", "exeggutor", "cubone", "marowak", "hitmonlee", "hitmonchan", "lickitung",
				"koffing", "weezing", "rhyhorn", "rhydon", "chansey", "tangela", "kangaskhan", "horsea",
				"seadra", "goldeen", "seaking", "staryu", "starmie", "mr. mime", "scyther", "jynx",
				"electabuzz", "magmar", "pinsir", "tauros", "magikarp", "gyarados", "lapras", "ditto",
				"eevee", "vaporeon", "jolteon", "flareon", "porygon", "omanyte", "omastar", "kabuto",
				"kabutops", "aerodactyl", "snorlax", "articuno", "zapdos", "moltres", "dratini", "dragonair",
				"dragonite", "mewtwo", "mew"
			})
		};

		crucible::hash_map<crucible::string, std::size_t> pokemon;

		for (std::size_t i { 0 }; i < sizeof(pokemon_names); ++i) {
			pokemon.put(pokemon_names[i], i);
		}

		for (std::size_t i { 0 }; i < sizeof(pokemon_names); ++i) {
			CRUCIBLE_ASSERT_EQ(pokemon.get(pokemon_names[i]), i);
		}
	}

	CRUCIBLE_TEST_SCENARIO(hash_map_iterators)
	{
		auto const noble_gas_atomic_masses {
			crucible::make_hash_map<std::size_t, double>({
					{ 2, 4.00e0 }, { 10, 2.09e1 }, { 18, 3.99e1 }, { 36, 8.38e1 }, { 54, 1.31e2 },
					{ 86, 2.22e2 }, { 118, 2.95e2 }
			})
		};

		// STL iterators
		double sum_of_atomic_masses { 0.0 };
		for (auto const &entry : noble_gas_atomic_masses) {
			sum_of_atomic_masses += entry.second;
		}

		CRUCIBLE_ASSERT_LT(std::abs(sum_of_atomic_masses - (4.00e0 + 2.09e1 + 3.99e1 + 8.38e1 + 1.31e2 + 2.22e2 + 2.95e2)), 0.0001);

		// Crucible iterators
		std::size_t const sum_of_atomic_numbers {
			crucible::iterate(noble_gas_atomic_masses)
				.fold([](std::size_t sum, std::pair<std::size_t, double> const &element) {
					return sum + element.first;
				}, static_cast<std::size_t>(0))
		};

		CRUCIBLE_ASSERT_EQ(sum_of_atomic_numbers, 2 + 10 + 18 + 36 + 54 + 86 + 118);
	}
}

CRUCIBLE_TEST_MAIN