1 module HashSetDemo;
2 
3 import common;
4 
5 import hunt.collection.HashMap;
6 import hunt.collection.HashSet;
7 import hunt.collection.Set;
8 import hunt.collection.Iterator;
9 
10 import std.stdio;
11 import std.conv;
12 import std.range;
13 
14 class HashSetDemo
15 {
16     void testBasicOperations()
17     {
18          HashSet!string hs = new HashSet!string();
19         //add elements to HashSet
20         hs.add("first");
21         hs.add("second");
22         hs.add("third");
23         writeln(hs);
24         writeln("Is HashSet empty? " ~ hs.isEmpty().to!string());
25         assert(!hs.isEmpty());
26         assert(hs.size == 3);
27 
28         HashSet!string subSet = new HashSet!string();
29 		subSet.add("s1");
30 		subSet.add("s2");
31 		hs.addAll(subSet);
32 		writeln("HashSet content after adding another collection:");
33 		writeln(hs);
34         assert(hs.size == 5);
35 
36         hs.remove("third");
37         writeln("\nremoving...");
38         writeln(hs);
39         writeln("Size of the HashSet: " ~ hs.size().to!string());
40         writeln("Does HashSet contains first element? " ~ hs.contains("first").to!string());
41         assert(hs.size == 4);
42 
43 
44 		writeln("Clearing HashSet:");
45 		hs.clear();
46 		writeln("Content After clear:");
47 		writeln(hs);
48         assert(hs.size == 0);
49    
50     }
51 
52     void testCompare()
53     {
54         HashSet!string hs = new HashSet!string();
55 		//add elements to HashSet
56 		hs.add("first");
57 		hs.add("second");
58 		hs.add("third");
59 		hs.add("apple");
60 		hs.add("rat");
61 		writeln(hs);
62 
63         assert(hs.size == 5);
64 
65 		HashSet!string subSet = new HashSet!string();
66 		subSet.add("rat");
67 		subSet.add("second");
68 		subSet.add("first");
69 		hs.retainAll(subSet);
70 		writeln("HashSet content:");
71 		writeln(hs);
72         assert(hs.size == 3);
73     }
74 
75     void testObjectSet()
76     {
77         HashSet!Price lhs = new HashSet!Price();
78 		lhs.add(new Price("Banana", 20));
79 		lhs.add(new Price("Apple", 40));
80 		lhs.add(new Price("Orange", 30));
81 		foreach(Price pr; lhs){
82 			writeln(pr);
83 		}
84         assert(lhs.size == 3);
85 
86 		Price duplicate = new Price("Banana", 20);
87 		writeln("\ninserting duplicate object...");
88 		lhs.add(duplicate);
89 		writeln("After insertion:");
90         assert(lhs.size == 3);
91 		foreach(Price pr; lhs){
92 			writeln(pr);
93 		}
94 
95 
96 		Price key = new Price("Banana", 20);
97 		writeln("Does set contains key? " ~ lhs.contains(key).to!string());
98 
99         writeln("\ndeleting key from set...");
100 		lhs.remove(key);
101 		writeln("Elements after delete:");
102         assert(lhs.size == 2);
103 		foreach(Price pr; lhs){
104 			writeln(pr);
105 		}
106 
107 		writeln("Does set contains key? " ~ lhs.contains(key).to!string());
108     }
109 }