| 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 ArrayListLong |
|---|
| 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[\Any\]) = 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 | (* Cause mild stress *) |
|---|
| 85 | testLens : List[\ZZ32\] = <|0 asif ZZ32,1,2,3,7,10,25,75,175,900|> |
|---|
| 86 | assert(1198, SUM testLens, "sum wrong") |
|---|
| 87 | for i <- seq(testLens) do |
|---|
| 88 | print( // i ":") |
|---|
| 89 | li = list(0#i) |
|---|
| 90 | chkPop(li,i) |
|---|
| 91 | chkExtract(li,i) |
|---|
| 92 | print(".") |
|---|
| 93 | assert(li,li,"Lists unequal") |
|---|
| 94 | assert(li CMP li,EqualTo,"Lists CMP unequal") |
|---|
| 95 | chkPop(li[#],i) |
|---|
| 96 | chkPop(li.addRight(i),i+1) |
|---|
| 97 | chkPop(li.addLeft(-1),-1,i+1) |
|---|
| 98 | (ll,lr) = li.split() |
|---|
| 99 | print(".") |
|---|
| 100 | lii = ll || lr |
|---|
| 101 | chkPop(lii,i) |
|---|
| 102 | print(".") |
|---|
| 103 | fli = li.filter(fn x => x REM 2 = 0) |
|---|
| 104 | print(".") |
|---|
| 105 | chkPop(fli,0,(i+1) DIV 2,2) |
|---|
| 106 | print(".") |
|---|
| 107 | flii = <|[\ZZ32\] x+3 | x <- li, x REM 2 = 0 |> |
|---|
| 108 | print(".") |
|---|
| 109 | chkPop(flii,3,(i+1) DIV 2,2) |
|---|
| 110 | print(".") |
|---|
| 111 | mli = li.map[\ZZ32\](fn (x):ZZ32 => x + 17) |
|---|
| 112 | chkPop(mli,17,i) |
|---|
| 113 | print(".") |
|---|
| 114 | assertStr(i=0 OR: li < mli," NOT < ",mli) |
|---|
| 115 | print(".") |
|---|
| 116 | cmli = li.concatMap[\ZZ32\](fn (x):List[\ZZ32\] => <|2 x asif ZZ32, 2 x + 1|>) |
|---|
| 117 | print(".") |
|---|
| 118 | chkPop(cmli,2 i) |
|---|
| 119 | print(".") |
|---|
| 120 | cmlii = <|[\ZZ32\] e+3 | x <- li, e <- <|2 x, 2 x + 1|> |> |
|---|
| 121 | print(".") |
|---|
| 122 | chkPop(cmlii,3,2 i,1) |
|---|
| 123 | print(".") |
|---|
| 124 | ivli = li.ivmap[\ZZ32\](fn (x,y) => x+y) |
|---|
| 125 | chkPop(ivli,0,i,2) |
|---|
| 126 | print(".") |
|---|
| 127 | rli=li.reverse |
|---|
| 128 | for j <- 0#i do |
|---|
| 129 | print(".") |
|---|
| 130 | assertStr(li[j],j,"indexing wrong for ",li) |
|---|
| 131 | assertStr(rli[i-j-1],j,"reverse indexing wrong for ",rli) |
|---|
| 132 | end |
|---|
| 133 | for j <- 0#(i+1) do |
|---|
| 134 | print(".") |
|---|
| 135 | (l,r) = li.split(j) |
|---|
| 136 | chkPop(l,j) |
|---|
| 137 | chkPop(r,j,i-j) |
|---|
| 138 | (l1,r1) = (li.take(j),li.drop(j)) |
|---|
| 139 | chkPop(l1,j) |
|---|
| 140 | chkPop(r1,j,i-j) |
|---|
| 141 | (l2,r2) = (li[0#j],li[j#(i-j)]) |
|---|
| 142 | chkPop(l2,j) |
|---|
| 143 | chkPop(r2,j,i-j) |
|---|
| 144 | (l3,r3) = (li[#j],li[j#]) |
|---|
| 145 | chkPop(l3,j) |
|---|
| 146 | chkPop(r3,j,i-j) |
|---|
| 147 | (l4,r4) = (li[:(j-1)],li[j:]) |
|---|
| 148 | chkPop(l4,j) |
|---|
| 149 | chkPop(r4,j,i-j) |
|---|
| 150 | end |
|---|
| 151 | for j <- testLens do |
|---|
| 152 | print(".") |
|---|
| 153 | lj = list(i#j) |
|---|
| 154 | chkPop(li || lj, i+j) |
|---|
| 155 | print(".") |
|---|
| 156 | gz = li.zip[\ZZ32\](lj) |
|---|
| 157 | assert(|gz|,|li| MIN |lj|, "zip size wrong") |
|---|
| 158 | assertStr(BIG AND [(x,y)<-gz] x+i=y,"zip mismatch ",li,lj) |
|---|
| 159 | print(".") |
|---|
| 160 | assertStr(j=0 OR: li < lj, " NOT < ", lj) |
|---|
| 161 | end |
|---|
| 162 | end |
|---|
| 163 | finish = nanoTime() |
|---|
| 164 | ms = ((finish-start) + 500000) DIV 1000000 |
|---|
| 165 | println( //"Succeeded in " (ms/1000.0) "s") |
|---|
| 166 | end |
|---|
| 167 | |
|---|
| 168 | end |
|---|