| 1 | (******************************************************************************* |
|---|
| 2 | Copyright 2008 Sun Microsystems, Inc., |
|---|
| 3 | 4150 Network Circle, Santa Clara, California 95054, U.S.A. |
|---|
| 4 | All rights reserved. |
|---|
| 5 | |
|---|
| 6 | U.S. Government Rights - Commercial software. |
|---|
| 7 | Government users are subject to the Sun Microsystems, Inc. standard |
|---|
| 8 | license agreement and applicable provisions of the FAR and its supplements. |
|---|
| 9 | |
|---|
| 10 | Use is subject to license terms. |
|---|
| 11 | |
|---|
| 12 | This distribution may include materials developed by third parties. |
|---|
| 13 | |
|---|
| 14 | Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered |
|---|
| 15 | trademarks of Sun Microsystems, Inc. in the U.S. and other countries. |
|---|
| 16 | ******************************************************************************) |
|---|
| 17 | |
|---|
| 18 | component CordedStringTests |
|---|
| 19 | import CordedString.{...} |
|---|
| 20 | import JavaString.{...} |
|---|
| 21 | import Set.{...} |
|---|
| 22 | import List.{...} |
|---|
| 23 | export Executable |
|---|
| 24 | |
|---|
| 25 | test testDepth(): () = do |
|---|
| 26 | words = ⟨"Hello ", "world, ", "it's ", "a ", "bright ", "new ", "day."⟩ |
|---|
| 27 | var result: String = EmptyString |
|---|
| 28 | for w ← seq(words) do result := CatString(result, w) end |
|---|
| 29 | assert(result, "Hello world, it's a bright new day.") |
|---|
| 30 | result.verify() |
|---|
| 31 | (* result.showStructure() *) |
|---|
| 32 | assert(result.depth, 7) |
|---|
| 33 | end |
|---|
| 34 | |
|---|
| 35 | test indexing(): () = do |
|---|
| 36 | words = ⟨"The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog."⟩ |
|---|
| 37 | var testString: String = EmptyString |
|---|
| 38 | for w ← seq(words) do testString := CatString(testString, w) end |
|---|
| 39 | referenceString = "Thequickbrownfoxjumpedoverthelazydog." |
|---|
| 40 | assert(testString, referenceString) |
|---|
| 41 | for i ← ⟨ 0, 5, 7, 8, 9, 36, 23, 35 ⟩ do |
|---|
| 42 | assert(testString[i], referenceString[i]) |
|---|
| 43 | end |
|---|
| 44 | for i ← ⟨ -1, -5, 37, 38, 109 ⟩ do |
|---|
| 45 | shouldRaise⟦IndexOutOfBounds⟧ (fn() => testString[i]) |
|---|
| 46 | end |
|---|
| 47 | end |
|---|
| 48 | |
|---|
| 49 | test subString(): () = do |
|---|
| 50 | words = ⟨"The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog."⟩ |
|---|
| 51 | var testString: String = EmptyString |
|---|
| 52 | for w ← seq(words) do testString := CatString(testString, w) end |
|---|
| 53 | referenceString = "Thequickbrownfoxjumpedoverthelazydog." |
|---|
| 54 | assert(testString, referenceString) |
|---|
| 55 | for i ← ⟨ 0, 5, 7, 8, 9, 36, 23, 35 ⟩, |
|---|
| 56 | j ← ⟨ 0, 5, 7, 8, 9, 36, 23, 35 ⟩ do |
|---|
| 57 | assert(testString[i:j], referenceString[i:j]) |
|---|
| 58 | end |
|---|
| 59 | end |
|---|
| 60 | |
|---|
| 61 | test subStringSimplification():() = do |
|---|
| 62 | testString = catStringFrom("The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog.") |
|---|
| 63 | testString.verify() |
|---|
| 64 | sub1 = testString[34:36] |
|---|
| 65 | typecase sub1 of |
|---|
| 66 | JavaString => assert(true) |
|---|
| 67 | String => do sub1.showStructure() |
|---|
| 68 | assert(false, "sub1=" sub1 " is not a JavaString") end |
|---|
| 69 | end |
|---|
| 70 | end |
|---|
| 71 | |
|---|
| 72 | (* test *) validateSubdivision(subject: String, division: Maybe⟦Generator⟦(ZZ32, String)⟧⟧): () = do |
|---|
| 73 | var len: ZZ32 = 0 |
|---|
| 74 | var accum: String = "" |
|---|
| 75 | if gen ← division then |
|---|
| 76 | for (start, str) ← seq(gen) do |
|---|
| 77 | assert(start, len, str) |
|---|
| 78 | len := len + |str| |
|---|
| 79 | accum := accum || str |
|---|
| 80 | end |
|---|
| 81 | assert(len, |subject|) |
|---|
| 82 | assert(accum, subject) |
|---|
| 83 | end |
|---|
| 84 | end |
|---|
| 85 | |
|---|
| 86 | test testSubdivide():() = do |
|---|
| 87 | subject = CatString("abcd", "efgh") |
|---|
| 88 | substr1 = subject[2:6] |
|---|
| 89 | var len: ZZ32 = 0 |
|---|
| 90 | var accum: String = "" |
|---|
| 91 | for (start, str) ← seq(substr1.subdivide()) do |
|---|
| 92 | assert(start, len, str) |
|---|
| 93 | len := len + |str| |
|---|
| 94 | accum := accum || str |
|---|
| 95 | end |
|---|
| 96 | assert(len, |substr1|) |
|---|
| 97 | accum.verify() |
|---|
| 98 | accum.showStructure() |
|---|
| 99 | assert(accum, substr1) |
|---|
| 100 | |
|---|
| 101 | substr2 = subject[4:6] |
|---|
| 102 | assert(substr2.subdivide(), Nothing⟦Generator⟦(ZZ32,String)⟧⟧) |
|---|
| 103 | |
|---|
| 104 | substr3 = subject[0:5] |
|---|
| 105 | for p ← seq(substr1.subdivide()) do |
|---|
| 106 | println ("found tuple" || p) (* This should (did?) work prefix string too *) |
|---|
| 107 | end |
|---|
| 108 | |
|---|
| 109 | end |
|---|
| 110 | |
|---|
| 111 | (* |
|---|
| 112 | test comparison():() = do |
|---|
| 113 | smaller = catStringFrom("The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog.") |
|---|
| 114 | bigger = catStringFrom("The", "quick", "brown", "jumped", "fox", "over", "the", "lazy", "doggie.") |
|---|
| 115 | smaller.verify |
|---|
| 116 | bigger.verify |
|---|
| 117 | smaller.showStructure |
|---|
| 118 | bigger.showStructure |
|---|
| 119 | assert(smaller < bigger) |
|---|
| 120 | assert(smaller[10:] < bigger[10:]) |
|---|
| 121 | a = smaller[30:36] |
|---|
| 122 | b = bigger[30:36] |
|---|
| 123 | a.verify |
|---|
| 124 | b.verify |
|---|
| 125 | a.showStructure |
|---|
| 126 | b.showStructure |
|---|
| 127 | assert(smaller[30:36] = bigger[30:36], smaller[30:36] " is not = to " bigger[30:36]) |
|---|
| 128 | end |
|---|
| 129 | *) |
|---|
| 130 | object CatStringReduction extends MonoidReduction[\String\] |
|---|
| 131 | getter toString() = "CatStringReduction" |
|---|
| 132 | empty(): String = EmptyString |
|---|
| 133 | join(a:String, b:String):String = CatString(a, b) |
|---|
| 134 | end |
|---|
| 135 | |
|---|
| 136 | test catStringFrom(args: String...): String = |
|---|
| 137 | seq(args).generate⟦String⟧(CatStringReduction, fn(x) => x) |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | test testNonEmptyConcat(): () = do |
|---|
| 141 | stuff = "Hello " |
|---|
| 142 | more = "World" |
|---|
| 143 | hw = CatString(stuff, more) |
|---|
| 144 | assert(|hw|, |stuff| + |more|) |
|---|
| 145 | assert(hw, "Hello World") |
|---|
| 146 | assert(hw, CatString("Hell", "o World")) |
|---|
| 147 | end |
|---|
| 148 | |
|---|
| 149 | test testEmptyConcat(): () = do |
|---|
| 150 | e = EmptyString |
|---|
| 151 | assert(|e|, 0) |
|---|
| 152 | assert(e.isEmpty) |
|---|
| 153 | stuff = "Hello " |
|---|
| 154 | more = "World" |
|---|
| 155 | h = CatString(stuff, e) |
|---|
| 156 | assert(|h|, |stuff|) |
|---|
| 157 | assert(stuff || e, stuff) |
|---|
| 158 | end |
|---|
| 159 | |
|---|
| 160 | test testCharConcat(): () = do |
|---|
| 161 | var result: String = EmptyString |
|---|
| 162 | hw = "Hello World" |
|---|
| 163 | for c <- seq(hw) do |
|---|
| 164 | result := result || c |
|---|
| 165 | end |
|---|
| 166 | assert(result, hw) |
|---|
| 167 | end |
|---|
| 168 | |
|---|
| 169 | test testParallelGenerator(): () = do |
|---|
| 170 | chars = "abcdefghijklmnopqrstuvwxyz" |
|---|
| 171 | var result: Set⟦Char⟧ = set⟦Char⟧() |
|---|
| 172 | for c ← chars atomic do result := result.add(c) end |
|---|
| 173 | assert(|result|, |chars|) |
|---|
| 174 | for c ← result do assert(c ∈ chars) end |
|---|
| 175 | end |
|---|
| 176 | |
|---|
| 177 | test testSequentialGenerator(): () = do |
|---|
| 178 | chars = "abcdefghijklmnopqrstuvwxyz" |
|---|
| 179 | var result: List⟦Char⟧ = emptyList⟦Char⟧(26) |
|---|
| 180 | for c ← seq(chars) do result := result.addRight(c) end |
|---|
| 181 | assert(|result|, |chars|) |
|---|
| 182 | for i ← result.bounds do assert(result[i] , chars[i]) end |
|---|
| 183 | end |
|---|
| 184 | |
|---|
| 185 | run(args:String...):() = do |
|---|
| 186 | println "Tests complete" |
|---|
| 187 | end |
|---|
| 188 | |
|---|
| 189 | end |
|---|