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