| 1 | (******************************************************************************* |
|---|
| 2 | Copyright 2009 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 ArrayListQuick |
|---|
| 19 | import List.{...} |
|---|
| 20 | export Executable |
|---|
| 21 | |
|---|
| 22 | assertStr[\T\](a:ZZ32, b:ZZ32, s:String, l: List[\T\]) = |
|---|
| 23 | if a=/=b then |
|---|
| 24 | fail(a "=/=" b ": " s l.asString) |
|---|
| 25 | end |
|---|
| 26 | |
|---|
| 27 | assertStr[\T\](a:Boolean, s:String, l: List[\T\]) = |
|---|
| 28 | if NOT a then |
|---|
| 29 | fail("Failed assertion: " s l.asString) |
|---|
| 30 | end |
|---|
| 31 | |
|---|
| 32 | assertStr[\T\](a:Boolean, s:String, l: List[\T\], r: List[\T\]) = |
|---|
| 33 | if NOT a then |
|---|
| 34 | fail("Failed assertion: " s l.asString " and " r.asString) |
|---|
| 35 | end |
|---|
| 36 | |
|---|
| 37 | chkApp(tl: List[\ZZ32\], t : List[\Object\]) = do |
|---|
| 38 | assert("Hello" IN t, true, "missing Hello from ", t) |
|---|
| 39 | assert(l IN t, true, "missing ",l," from ",t), l <- tl |
|---|
| 40 | end |
|---|
| 41 | |
|---|
| 42 | chkPop[\T\](l:List[\T\],fro:ZZ32,sz:ZZ32,by:ZZ32): () = |
|---|
| 43 | typecase l of |
|---|
| 44 | List[\ZZ32\] => chkPopZ(l,fro,sz,by) |
|---|
| 45 | else => |
|---|
| 46 | assertStr(sz = 0 AND: |l| = 0," special case for empty comprehension",l) |
|---|
| 47 | end |
|---|
| 48 | |
|---|
| 49 | chkPopZ(l:List[\ZZ32\],fro:ZZ32,sz:ZZ32,by:ZZ32): () = do |
|---|
| 50 | assertStr(sz,|l|," size mismatch in ",l) |
|---|
| 51 | n : ZZ32 := fro |
|---|
| 52 | for e <- seq(l) do |
|---|
| 53 | assertStr(e,n,"Element wrong in ",l) |
|---|
| 54 | n+=by |
|---|
| 55 | end |
|---|
| 56 | assertStr(n, fro + sz by," size mismatch in ",l) |
|---|
| 57 | end |
|---|
| 58 | |
|---|
| 59 | chkPop(l:List[\ZZ32\],fro:ZZ32,sz:ZZ32): () = chkPopZ(l,fro,sz,1) |
|---|
| 60 | |
|---|
| 61 | chkPop(l:List[\ZZ32\],sz:ZZ32): () = chkPopZ(l,0,sz,1) |
|---|
| 62 | |
|---|
| 63 | chkExtract(l:List[\ZZ32\],sz:ZZ32): () = do |
|---|
| 64 | ll : List[\ZZ32\] := l |
|---|
| 65 | for i <- seq(0#sz) do |
|---|
| 66 | (e,ll0) = ll.extractLeft.get |
|---|
| 67 | assertStr(e,i,"extractLeft wrong in ",ll) |
|---|
| 68 | assertStr(ll.left.get,i,"left wrong in ",ll) |
|---|
| 69 | ll := ll0 |
|---|
| 70 | end |
|---|
| 71 | assertStr(ll.isEmpty," not empty after extractLeft; it's ",ll) |
|---|
| 72 | ll := l |
|---|
| 73 | for i <- seq(1#sz) do |
|---|
| 74 | (ll0,e) = ll.extractRight.get |
|---|
| 75 | assertStr(e,sz-i,"extractRight wrong in ",ll) |
|---|
| 76 | assertStr(ll.right.get,sz-i,"right wrong in ",ll) |
|---|
| 77 | ll := ll0 |
|---|
| 78 | end |
|---|
| 79 | assertStr(ll.isEmpty," not empty after extractRight; it's ",ll) |
|---|
| 80 | end |
|---|
| 81 | |
|---|
| 82 | run():() = do |
|---|
| 83 | start = nanoTime() |
|---|
| 84 | (* The sizes which turned up bugs *) |
|---|
| 85 | testLens : List[\ZZ32\] = <|0 asif ZZ32,1,2,7,10|> |
|---|
| 86 | assert(20,SUM testLens,"sum wrong") |
|---|
| 87 | for i <- seq(testLens) do |
|---|
| 88 | print( // i ":") |
|---|
| 89 | li = list(0#i) |
|---|
| 90 | print(li) |
|---|
| 91 | chkPop(li,i) |
|---|
| 92 | chkExtract(li,i) |
|---|
| 93 | print(".") |
|---|
| 94 | assert(li,li,"Lists unequal") |
|---|
| 95 | assert(li CMP li,EqualTo,"Lists CMP unequal") |
|---|
| 96 | chkPop(li[#],i) |
|---|
| 97 | chkPop(li.addRight(i),i+1) |
|---|
| 98 | chkPop(li.addLeft(-1),-1,i+1) |
|---|
| 99 | (ll,lr) = li.split() |
|---|
| 100 | print(".") |
|---|
| 101 | lii = ll || lr |
|---|
| 102 | chkPop(lii,i) |
|---|
| 103 | print(".") |
|---|
| 104 | fli = li.filter(fn x => x REM 2 = 0) |
|---|
| 105 | print(".") |
|---|
| 106 | chkPop(fli,0,(i+1) DIV 2,2) |
|---|
| 107 | print(".") |
|---|
| 108 | flii = <|[\ZZ32\] x+3 | x <- li, x REM 2 = 0 |> |
|---|
| 109 | print(".") |
|---|
| 110 | chkPop(flii,3,(i+1) DIV 2,2) |
|---|
| 111 | print(".") |
|---|
| 112 | mli = li.map[\ZZ32\](fn (x):ZZ32 => x + 17) |
|---|
| 113 | chkPop(mli,17,i) |
|---|
| 114 | print(".") |
|---|
| 115 | assertStr(i=0 OR: li < mli," NOT < ",mli) |
|---|
| 116 | print(".") |
|---|
| 117 | cmli = li.concatMap[\ZZ32\](fn (x):List[\ZZ32\] => <|2 x asif ZZ32, 2 x + 1|>) |
|---|
| 118 | print(".") |
|---|
| 119 | chkPop(cmli,2 i) |
|---|
| 120 | print(".") |
|---|
| 121 | cmlii = <|[\ZZ32\] e+3 | x <- li, e <- <|2 x, 2 x + 1|> |> |
|---|
| 122 | print(".") |
|---|
| 123 | chkPop(cmlii,3,2 i,1) |
|---|
| 124 | print(".") |
|---|
| 125 | ivli = li.ivmap[\ZZ32\](fn (x,y) => x+y) |
|---|
| 126 | chkPop(ivli,0,i,2) |
|---|
| 127 | print(".") |
|---|
| 128 | rli=li.reverse |
|---|
| 129 | for j <- 0#i do |
|---|
| 130 | print(".") |
|---|
| 131 | assertStr(li[j],j,"indexing wrong for ",li) |
|---|
| 132 | assertStr(rli[i-j-1],j,"reverse indexing wrong for ",rli) |
|---|
| 133 | end |
|---|
| 134 | for j <- 0#(i+1) do |
|---|
| 135 | print(".") |
|---|
| 136 | (l,r) = li.split(j) |
|---|
| 137 | chkPop(l,j) |
|---|
| 138 | chkPop(r,j,i-j) |
|---|
| 139 | (l1,r1) = (li.take(j),li.drop(j)) |
|---|
| 140 | chkPop(l1,j) |
|---|
| 141 | chkPop(r1,j,i-j) |
|---|
| 142 | (l2,r2) = (li[0#j],li[j#(i-j)]) |
|---|
| 143 | chkPop(l2,j) |
|---|
| 144 | chkPop(r2,j,i-j) |
|---|
| 145 | (l3,r3) = (li[#j],li[j#]) |
|---|
| 146 | chkPop(l3,j) |
|---|
| 147 | chkPop(r3,j,i-j) |
|---|
| 148 | (l4,r4) = (li[:(j-1)],li[j:]) |
|---|
| 149 | chkPop(l4,j) |
|---|
| 150 | chkPop(r4,j,i-j) |
|---|
| 151 | end |
|---|
| 152 | for j <- testLens do |
|---|
| 153 | print(".") |
|---|
| 154 | lj = list(i#j) |
|---|
| 155 | chkPop(li || lj, i+j) |
|---|
| 156 | print(".") |
|---|
| 157 | gz = li.zip[\ZZ32\](lj) |
|---|
| 158 | assert(|gz|,|li| MIN |lj|, "zip size wrong") |
|---|
| 159 | assertStr(BIG AND [(x,y)<-gz] x+i=y,"zip mismatch ",li,lj) |
|---|
| 160 | print(".") |
|---|
| 161 | assertStr(j=0 OR: li < lj, " NOT < ", lj) |
|---|
| 162 | end |
|---|
| 163 | end |
|---|
| 164 | testStr : List[\String\] = <| "Hello" asif String |> |
|---|
| 165 | testU : List[\Object\] = testStr || testLens |
|---|
| 166 | chkApp(testLens,testU) |
|---|
| 167 | chkApp(testLens,(testU || testStr).drop(1)) |
|---|
| 168 | chkApp(testLens,(testStr || testU).drop(1)) |
|---|
| 169 | chkApp(testLens,testLens.addLeft("Hello")) |
|---|
| 170 | chkApp(testLens,testLens.addRight("Hello")) |
|---|
| 171 | finish = nanoTime() |
|---|
| 172 | ms = ((finish-start) + 500000) DIV 1000000 |
|---|
| 173 | println( //"Succeeded in " (ms/1000.0) "s") |
|---|
| 174 | end |
|---|
| 175 | |
|---|
| 176 | end |
|---|