| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | package com.sun.fortress.useful; |
|---|
| 19 | |
|---|
| 20 | import java.util.Collection; |
|---|
| 21 | import java.util.Collections; |
|---|
| 22 | import java.util.HashSet; |
|---|
| 23 | import java.util.List; |
|---|
| 24 | import java.util.Set; |
|---|
| 25 | |
|---|
| 26 | import junit.framework.TestCase; |
|---|
| 27 | |
|---|
| 28 | public class UsefulJUTest extends com.sun.fortress.useful.TestCaseWrapper { |
|---|
| 29 | |
|---|
| 30 | public UsefulJUTest() { |
|---|
| 31 | super("UsefulJUTest"); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | public static void main(String[] args) { |
|---|
| 35 | junit.swingui.TestRunner.run(UsefulJUTest.class); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | static List<String> wordsList = Useful.list("cheese", "mouse", "cat", "dog"); |
|---|
| 39 | static List<String> moreWordsList = Useful.list("kelp", "urchin", "cod", "shark"); |
|---|
| 40 | static List<String> noWordsList = Collections.<String>emptyList(); |
|---|
| 41 | |
|---|
| 42 | static Set<List<String>> house = Useful.<List<String>>set(wordsList); |
|---|
| 43 | static Set<List<String>> sea = Useful.<List<String>>set(moreWordsList); |
|---|
| 44 | static Set<List<String>> empty = Useful.<List<String>>set(noWordsList); |
|---|
| 45 | static Set<List<String>> houseSea = Useful.<List<String>>set(wordsList, moreWordsList); |
|---|
| 46 | static Set<List<String>> houseSeaEmpty = Useful.<List<String>>set(wordsList, moreWordsList, noWordsList); |
|---|
| 47 | |
|---|
| 48 | static Set<String> wordsSet = new HashSet<String>(wordsList); |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | public void testSetProductSetOfListOfTSetOfT() { |
|---|
| 54 | Set<List<String>> a = Useful.setProduct(house, Collections.<String>emptySet()); |
|---|
| 55 | assertEquals(Collections.emptySet(), a); |
|---|
| 56 | |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | public final static Fn2<List<String>, List<String>, List<String>> listAppender = |
|---|
| 60 | Fn2.<String>listAppender(); |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | public void testSetProductSetOfTSetOfUFn2OfTUV() { |
|---|
| 66 | Set<List<String>> x = Useful.<List<String>,List<String>,List<String>>setProduct( |
|---|
| 67 | empty, Collections.<List<String>>emptySet(), listAppender |
|---|
| 68 | ); |
|---|
| 69 | assertEquals(Collections.emptySet(), x); |
|---|
| 70 | System.out.println(x); |
|---|
| 71 | |
|---|
| 72 | x = Useful.<List<String>,List<String>,List<String>>setProduct( |
|---|
| 73 | Collections.<List<String>>emptySet(), empty, listAppender |
|---|
| 74 | ); |
|---|
| 75 | assertEquals(Collections.emptySet(), x); |
|---|
| 76 | System.out.println(x); |
|---|
| 77 | |
|---|
| 78 | x = Useful.<List<String>,List<String>,List<String>>setProduct( |
|---|
| 79 | Collections.<List<String>>emptySet(), Collections.<List<String>>emptySet(), listAppender |
|---|
| 80 | ); |
|---|
| 81 | assertEquals(Collections.emptySet(), x); |
|---|
| 82 | System.out.println(x); |
|---|
| 83 | |
|---|
| 84 | x = Useful.setProduct(sea, empty, listAppender); |
|---|
| 85 | assertEquals(sea, x); |
|---|
| 86 | System.out.println(x); |
|---|
| 87 | |
|---|
| 88 | x = Useful.setProduct(empty, empty, listAppender); |
|---|
| 89 | assertEquals(empty, x); |
|---|
| 90 | System.out.println(x); |
|---|
| 91 | |
|---|
| 92 | x = Useful.setProduct( Useful.set(Useful.list("a"), Useful.list("b")), |
|---|
| 93 | Useful.set(Useful.list("c"), Useful.list("d")) , |
|---|
| 94 | listAppender); |
|---|
| 95 | assertEquals( Useful.set(VarArgs.make(Useful.list("a", "c"), |
|---|
| 96 | Useful.list("a", "d"), |
|---|
| 97 | Useful.list("b", "c"), |
|---|
| 98 | Useful.list("b", "d"))), x); |
|---|
| 99 | System.out.println(x); |
|---|
| 100 | |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | public void testSingleton() { |
|---|
| 107 | |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | public void testListInParens() { |
|---|
| 114 | assertEquals("(cheese,mouse,cat,dog)", Useful.listInParens(wordsList)); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | public void testListInCurlies() { |
|---|
| 121 | assertEquals("{cheese,mouse,cat,dog}", Useful.listInCurlies(wordsList)); |
|---|
| 122 | |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | public void testListInDelimiters() { |
|---|
| 129 | assertEquals("<<<cheese,mouse,cat,dog>>>", Useful.listInDelimiters("<<<", wordsList, ">>>")); |
|---|
| 130 | assertEquals("<<<>>>", Useful.listInDelimiters("<<<", noWordsList, ">>>")); |
|---|
| 131 | |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | public void testListsInParens() { |
|---|
| 138 | assertEquals("(cheese,mouse,cat,dog,kelp,urchin,cod,shark)", |
|---|
| 139 | Useful.listsInParens(wordsList, moreWordsList)); |
|---|
| 140 | assertEquals("(kelp,urchin,cod,shark)", |
|---|
| 141 | Useful.listsInParens(noWordsList, moreWordsList)); |
|---|
| 142 | assertEquals("(cheese,mouse,cat,dog)", |
|---|
| 143 | Useful.listsInParens(wordsList, noWordsList)); |
|---|
| 144 | assertEquals("()", |
|---|
| 145 | Useful.listsInParens(noWordsList, noWordsList)); |
|---|
| 146 | |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | public void testDottedList() { |
|---|
| 153 | assertEquals("cheese.mouse.cat.dog", Useful.dottedList(wordsList)); |
|---|
| 154 | |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | public void testConcat() { |
|---|
| 158 | List<String> both = Useful.concat(wordsList, moreWordsList); |
|---|
| 159 | assertEquals("(cheese,mouse,cat,dog,kelp,urchin,cod,shark)", |
|---|
| 160 | Useful.listInParens(both)); |
|---|
| 161 | assertEquals("(kelp,urchin,cod,shark)", |
|---|
| 162 | Useful.listInParens(Useful.concat(noWordsList, moreWordsList))); |
|---|
| 163 | assertEquals("(cheese,mouse,cat,dog)", |
|---|
| 164 | Useful.listInParens(Useful.concat(wordsList, noWordsList))); |
|---|
| 165 | assertEquals("()", |
|---|
| 166 | Useful.listInParens(Useful.concat(noWordsList, noWordsList))); |
|---|
| 167 | assertEquals(noWordsList, Useful.concat()); |
|---|
| 168 | assertEquals(noWordsList, Useful.concat(noWordsList)); |
|---|
| 169 | assertEquals(moreWordsList, Useful.concat(moreWordsList)); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | public void testUnion() { |
|---|
| 173 | Collection<List<String>> nullSet = Useful.<List<String>>set(); |
|---|
| 174 | assertEquals(nullSet, Useful.union(nullSet, nullSet)); |
|---|
| 175 | assertEquals(houseSeaEmpty, Useful.union(house,sea,empty)); |
|---|
| 176 | assertEquals(houseSeaEmpty, Useful.union(houseSea,empty)); |
|---|
| 177 | assertEquals(houseSeaEmpty, Useful.union(house,Useful.union(sea,empty))); |
|---|
| 178 | assertEquals(houseSea, Useful.union(nullSet, houseSea)); |
|---|
| 179 | assertEquals(houseSea, Useful.union(houseSea, nullSet)); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | public void testCountMatch() { |
|---|
| 183 | assertEquals(3, Useful.countMatches("aaa", "a")); |
|---|
| 184 | assertEquals(0, Useful.countMatches("", "a")); |
|---|
| 185 | assertEquals(0, Useful.countMatches("aaa", "b")); |
|---|
| 186 | assertEquals(3, Useful.countMatches("ababa", "a")); |
|---|
| 187 | } |
|---|
| 188 | public void testReplaceCount() { |
|---|
| 189 | assertEquals("aaa", Useful.replace("aaa", "a", "b", 0)); |
|---|
| 190 | assertEquals("aaa", Useful.replace("aaa", "c", "b", 3)); |
|---|
| 191 | assertEquals("ba", Useful.replace("aaa", "aa", "b", 3)); |
|---|
| 192 | assertEquals("bb", Useful.replace("aaaa", "aa", "b", 3)); |
|---|
| 193 | assertEquals("baa", Useful.replace("aaa", "a", "b", 1)); |
|---|
| 194 | assertEquals("bba", Useful.replace("aaa", "a", "b", 2)); |
|---|
| 195 | assertEquals("bbb", Useful.replace("aaa", "a", "b", 3)); |
|---|
| 196 | assertEquals("bbb", Useful.replace("aaa", "a", "b", 4)); |
|---|
| 197 | assertEquals("cba", Useful.replace("caa", "a", "b", 1)); |
|---|
| 198 | } |
|---|
| 199 | public void testHashBijection() { |
|---|
| 200 | HashBijection<String, Integer> map = new HashBijection<String, Integer>(); |
|---|
| 201 | Integer one = 1; |
|---|
| 202 | Integer two = 2; |
|---|
| 203 | Integer three = 3; |
|---|
| 204 | Integer threeA = 3; |
|---|
| 205 | |
|---|
| 206 | map.put("one", one); |
|---|
| 207 | map.put("two", two); |
|---|
| 208 | map.put("three", three); |
|---|
| 209 | |
|---|
| 210 | assertEquals(true, map.validate()); |
|---|
| 211 | |
|---|
| 212 | map.put("one", one); |
|---|
| 213 | map.put("two", two); |
|---|
| 214 | map.put("three", threeA); |
|---|
| 215 | assertEquals(true, map.validate()); |
|---|
| 216 | assertEquals(true, map.get("three") == threeA); |
|---|
| 217 | |
|---|
| 218 | map.put("too", two); |
|---|
| 219 | assertEquals(true, map.validate()); |
|---|
| 220 | |
|---|
| 221 | map.put("too", two); |
|---|
| 222 | assertEquals(true, map.validate()); |
|---|
| 223 | |
|---|
| 224 | map.put("too", three); |
|---|
| 225 | assertEquals(true, map.validate()); |
|---|
| 226 | |
|---|
| 227 | map.remove("too"); |
|---|
| 228 | assertEquals(true, map.validate()); |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | } |
|---|
| 232 | } |
|---|