1 module SetDemo;
2 
3 import std.stdio;
4 
5 import common;
6 import hunt.util.UnitTest;
7 import hunt.collection.Set;
8 import hunt.collection.SortedSet;
9 import hunt.collection.TreeSet;
10 
11 import hunt.Exceptions;
12 import hunt.logging.ConsoleLogger;
13 import hunt.util.UnitTest;
14 import hunt.util.Common;
15 import hunt.Assert;
16 
17 import std.conv;
18 import std.range;
19 
20 alias assertTrue = Assert.assertTrue;
21 alias assertFalse = Assert.assertFalse;
22 alias assertThat = Assert.assertThat;
23 alias assertEquals = Assert.assertEquals;
24 alias assertNotNull = Assert.assertNotNull;
25 alias assertNull = Assert.assertNull;
26 
27 class SetDemo {
28 
29     enum one = 1;
30     enum two = 2;
31     enum three = 3;
32     enum four = 4;
33     enum five = 5;
34 
35     /**
36      * Returns a new set of first 5 ints.
37      */
38     private static TreeSet!int set5() {
39         TreeSet!int q = new TreeSet!int();
40         assertTrue(q.isEmpty());
41         q.add(1);
42         q.add(3);
43         q.add(4);
44         q.add(2);
45         q.add(5);
46         assertEquals(5, q.size());
47         return q;
48     }
49 
50     void testSimpleTailSet() {
51         TreeSet!int set = new TreeSet!int();
52         set.add(0);
53         SortedSet!int sm = set.tailSet(0);
54         assert(sm.size() == 1);
55         sm = set.tailSet(1);
56         assert(sm.size() == 0);
57     }
58 
59     void testTailSetContents() {
60         TreeSet!int set = set5();
61         SortedSet!int sm = set.tailSet(two);
62         assertFalse(sm.contains(1));
63         assertTrue(sm.contains(2));
64         assertTrue(sm.contains(3));
65         assertTrue(sm.contains(4));
66         assertTrue(sm.contains(5));
67 
68         // trace(sm.toString());
69 
70         // InputRange!int i = sm.iterator();
71         // int k;
72         // k = (i.front());
73         // assertEquals(two, k);
74         // i.popFront();
75         // k = (i.front());
76         // assertEquals(three, k);
77         // i.popFront();
78         // k = (i.front());
79         // assertEquals(four, k);
80         // i.popFront();
81         // k = (i.front());
82         // assertEquals(five, k);
83         // i.popFront();
84         // assertTrue(i.empty());
85 
86         SortedSet!int ssm = sm.tailSet(four);
87         // trace(ssm.toString());
88         assertEquals(four, ssm.first());
89         assertEquals(five, ssm.last());
90         trace("ssm: ", ssm.toString());
91         trace("sm: ", sm.toString());
92         trace("set: ", set.toString());
93         assertTrue(ssm.remove(four));
94         assertEquals(1, ssm.size());
95         assertEquals(3, sm.size());
96         assertEquals(4, set.size());
97 
98         trace("ssm: ", ssm.toString());
99         trace("sm: ", sm.toString());
100         trace("set: ", set.toString());
101     }
102 
103     void testInt() {
104         TreeSet!(int) ts = new TreeSet!(int)();
105         ts.add(3);
106         ts.add(10);
107         ts.add(2);
108         writeln("Elements: " ~ ts.toString());
109         writeln("First element: " ~ to!string(ts.first()));
110     }
111 
112     void testTreeSetBasic() {
113         TreeSet!(string) ts = new TreeSet!(string)();
114         ts.add("one");
115         ts.add("two");
116         ts.add("three");
117 
118         assert(ts.size() == 3);
119         writeln("Elements: " ~ ts.toString());
120         writeln("First element: " ~ ts.first());
121 
122         //check is set empty?
123         writeln("Is set empty: " ~ ts.isEmpty().to!string());
124 
125         string[] values = ts.toArray();
126         writeln(values);
127 
128         writeln("Size of the set: " ~ ts.size().to!string());
129         //remove one string
130         ts.remove("two");
131         writeln("Elements: " ~ ts.toString());
132         assert(ts.size() == 2);
133 
134         //delete all elements from set
135         ts.clear();
136         writeln("Is set empty: " ~ ts.isEmpty().to!string());
137         writeln("Elements: " ~ ts.toString());
138         values = ts.toArray();
139         assert(values == []);
140         assert(ts.size() == 0);
141     }
142 
143     void testTreeSetWithClass() {
144 
145         TreeSet!Person treeSet = new TreeSet!Person();
146         treeSet.add(new Person("albert", 8));
147         treeSet.add(new Person("bob", 5));
148         treeSet.add(new Person("bob", 13));
149 
150         foreach (Person person; treeSet) {
151             writeln(person.toString());
152         }
153 
154         assert(treeSet.size() == 3, treeSet.size().to!string());
155         Person p = treeSet.first();
156         treeSet.remove(p);
157         assert(treeSet.size() == 2);
158     }
159 
160     void testTreeSetWithComparator() {
161         
162         ComparatorByPrice com = new ComparatorByPrice();
163 
164         TreeSet!(Price) ts = new TreeSet!(Price)(com);
165         ts.add(new Price("Banana", 20));
166         ts.add(new Price("Apple", 40));
167         ts.add(new Price("Orange", 30));
168         assert(ts.size() == 3, ts.size().to!string());
169         foreach (Price p; ts) {
170             writeln(p.toString());
171         }
172 
173         Price p = ts.first();
174         ts.remove(p);
175         assert(ts.size == 2);
176     }
177 
178     void testSubset() {
179         // http://java2novice.com/java-collections-and-util/treeset/subset/
180         TreeSet!string ts = new TreeSet!string();
181         ts.add("RED");
182         ts.add("ORANGE");
183         ts.add("BLUE");
184         ts.add("GREEN");
185         ts.add("WHITE");
186         ts.add("BROWN");
187         ts.add("YELLOW");
188         ts.add("BLACK");
189         writeln(ts);
190         Set!string subSet = ts.subSet("GREEN", "WHITE");
191         writeln("sub set: " ~ subSet.toString());
192         assert(subSet.toString() == "[GREEN, ORANGE, RED]", subSet.toString());
193 
194         subSet = ts.subSet("GREEN", true, "WHITE", true);
195         writeln("sub set: " ~ subSet.toString());
196         assert(subSet.toString() == "[GREEN, ORANGE, RED, WHITE]", subSet.toString());
197 
198         subSet = ts.subSet("GREEN", false, "WHITE", true);
199         writeln("sub set: " ~ subSet.toString());
200         assert(subSet.toString() == "[ORANGE, RED, WHITE]", subSet.toString());
201     }
202 
203 }