summaryrefslogtreecommitdiff
path: root/test/map.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2008-08-11 13:32:13 -0700
committerRob Pike <r@golang.org>2008-08-11 13:32:13 -0700
commit563aa32d836ff8da57cdefb91a7d3710e4a535f3 (patch)
treedb2a71c3e6d355f5dc3ad59725bf42bfed92aa9a /test/map.go
parent17c78ba50c98b32d6dfa8fc2b7dd1ef3ed04db94 (diff)
downloadgo-563aa32d836ff8da57cdefb91a7d3710e4a535f3.tar.gz
add test for maps.
R=gri OCL=14090 CL=14090
Diffstat (limited to 'test/map.go')
-rw-r--r--test/map.go531
1 files changed, 531 insertions, 0 deletions
diff --git a/test/map.go b/test/map.go
new file mode 100644
index 000000000..c5544e529
--- /dev/null
+++ b/test/map.go
@@ -0,0 +1,531 @@
+// $G $F.go && $L $F.$A && ./$A.out
+
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+
+package main
+
+import fmt "fmt"
+
+const arraylen = 2; // BUG: shouldn't need this
+
+func P(a *[]string) string {
+ s := "{";
+ for i := 0; i < len(a); i++ {
+ if i > 0 {
+ s += ","
+ }
+ s += `"` + a[i] + `"`;
+ }
+ s +="}";
+ return s;
+}
+
+func main() {
+ F := fmt.New();
+
+ // BUG: should test a map literal when there's syntax
+
+ mib := new(map[int] bool);
+ mii := new(map[int] int);
+ mfi := new(map[float] int);
+ mif := new(map[int] float);
+ msi := new(map[string] int);
+ mis := new(map[int] string);
+ mss := new(map[string] string);
+ mspa := new(map[string] *[]string);
+ // BUG need an interface map both ways too
+
+ type T struct {
+ s string;
+ f float;
+ };
+ mipT := new(map[int] *T);
+ mpTi := new(map[*T] int);
+ //mit := new(map[int] T); // should be able to do a value but: fatal error: algtype: cant find type <T>{}
+ //mti := new(map[T] int); // should be able to do a value but: fatal error: algtype: cant find type <T>{}
+
+ type M map[int] int;
+ mipM := new(map[int] *M);
+
+ const count = 100; // BUG: should be bigger but maps do linear lookup
+ var apT [2*count]*T;
+
+ for i := 0; i < count; i++ {
+ s := F.d(i).str();
+ f := float(i);
+ apT[i] = new(T);
+ apT[i].s = s;
+ apT[i].f = f;
+ apT[2*i] = new(T); // need twice as many entries as we use, for the nonexistence check
+ apT[2*i].s = s;
+ apT[2*i].f = f;
+ // BUG t := T(s, f);
+ t := new(T); t.s = s; t.f = f;
+ // BUG m := M(i, i+1);
+ m := new(M); m[i] = i+1;
+ mib[i] = (i != 0);
+ mii[i] = 10*i;
+ mfi[float(i)] = 10*i;
+ mif[i] = 10.0*f;
+ mis[i] = s;
+ msi[F.d(i).str()] = i;
+ mss[F.d(i).str()] = F.d(10*i).str();
+ mss[F.d(i).str()] = F.d(10*i).str();
+ as := new([arraylen]string);
+ as[0] = F.d(10*i).str();
+ as[1] = F.d(10*i).str();
+ mspa[F.d(i).str()] = as;
+ mipT[i] = t;
+ mpTi[apT[i]] = i;
+ // BUG mti[t] = i;
+ mipM[i] = m;
+ }
+
+ // test len
+ if len(mib) != count {
+ F.s("len(mib) = ").d(len(mib)).putnl();
+ }
+ if len(mii) != count {
+ F.s("len(mii) = ").d(len(mii)).putnl();
+ }
+ if len(mfi) != count {
+ F.s("len(mfi) = ").d(len(mfi)).putnl();
+ }
+ if len(mif) != count {
+ F.s("len(mif) = ").d(len(mif)).putnl();
+ }
+ if len(msi) != count {
+ F.s("len(msi) = ").d(len(msi)).putnl();
+ }
+ if len(mis) != count {
+ F.s("len(mis) = ").d(len(mis)).putnl();
+ }
+ if len(mss) != count {
+ F.s("len(mss) = ").d(len(mss)).putnl();
+ }
+ if len(mspa) != count {
+ F.s("len(mspa) = ").d(len(mspa)).putnl();
+ }
+ if len(mipT) != count {
+ F.s("len(mipT) = ").d(len(mipT)).putnl();
+ }
+ if len(mpTi) != count {
+ F.s("len(mpTi) = ").d(len(mpTi)).putnl();
+ }
+// if len(mti) != count {
+// F.s("len(mti) = ").d(len(mti)).putnl();
+// }
+ if len(mipM) != count {
+ F.s("len(mipM) = ").d(len(mipM)).putnl();
+ }
+
+ // test construction directly
+ for i := 0; i < count; i++ {
+ s := F.d(i).str();
+ f := float(i);
+ // BUG t := T(s, f);
+ var t T; t.s = s; t.f = f;
+ // BUG m := M(i, i+1);
+ if mib[i] != (i != 0) {
+ F.s("mib[").d(i).s("] = ").boolean(mib[i]).putnl();
+ }
+ if(mii[i] != 10*i) {
+ F.s("mii[").d(i).s("] = ").d(mii[i]).putnl();
+ }
+ if(mfi[f] != 10*i) {
+ F.s("mfi[").d(i).s("] = ").d(mfi[f]).putnl();
+ }
+ if(mif[i] != 10.0*f) {
+ F.s("mif[").d(i).s("] = ").g(mif[i]).putnl();
+ }
+ if(mis[i] != s) {
+ F.s("mis[").d(i).s("] = ").s(mis[i]).putnl();
+ }
+ if(msi[s] != i) {
+ F.s("msi[").s(s).s("] = ").d(msi[s]).putnl();
+ }
+ if mss[s] != F.d(10*i).str() {
+ F.s("mss[").s(s).s("] = ").s(mss[s]).putnl();
+ }
+ for j := 0; j < arraylen; j++ {
+ if mspa[s][j] != F.d(10*i).str() {
+ F.s("mspa[").s(s).s("][").d(j).s("] = ").s(mspa[s][j]).putnl();
+ }
+ }
+ if(mipT[i].s != s || mipT[i].f != f) {
+ F.s("mipT[").d(i).s("] = {").s(mipT[i].s).s(", ").g(mipT[i].f).s("}").putnl();
+ }
+ if(mpTi[apT[i]] != i) {
+ F.s("mpTi[apT[").d(i).s("]] = ").d(mpTi[apT[i]]).putnl();
+ }
+// if(mti[t] != i) {
+// F.s("mti[").s(s).s("] = ").s(mti[s]).putnl();
+// }
+ if (mipM[i][i] != i + 1) {
+ F.s("mipM[").d(i).s("][").d(i).s("] =").d(mipM[i][i]).putnl();
+ }
+ }
+
+ // test existence with zero check
+ // failed lookups yield the 'zero' of the type.
+ // a later loop uses the more reliable tuple check.
+ for i := 1; i < count; i++ { // loop from 1 to avoid the real 'zeros'
+ s := F.d(i).str();
+ f := float(i);
+ // BUG t := T(s, f);
+ var t T; t.s = s; t.f = f;
+ // BUG m := M(i, i+1);
+ if mib[i] == false {
+ F.s("existence: mib[").d(i).s("] == false").putnl();
+ }
+ if mii[i] == 0 {
+ F.s("existence: mii[").d(i).s("] == 0").putnl();
+ }
+ if mfi[f] == 0 {
+ F.s("existence: mfi[").g(f).s("] == 0").putnl();
+ }
+ if mif[i] == 0.0 {
+ F.s("existence: mif[").d(i).s("] == 0.0").putnl();
+ }
+ if mis[i] == "" {
+ F.s("existence: mis[").d(i).s("] == ``").putnl();
+ }
+ if msi[s] == 0 {
+ F.s("existence: msi[").s(s).s("] == 0").putnl();
+ }
+ if mss[s] == `` {
+ F.s("existence: mss[").s(s).s("] == ``").putnl();
+ }
+ if mspa[s] == nil {
+ F.s("existence: mspa[").s(s).s("] == ``").putnl();
+ }
+ if mipT[i] == nil {
+ F.s("existence: mipT[").d(i).s("] == nil").putnl();
+ }
+ if mpTi[apT[i]] == 0 {
+ F.s("existence: mpTi[apT[").d(i).s("]] == 0").putnl();
+ }
+// if mti[t] == 0 {
+// //emit stdout <- format("haskey mti[%s] false", string(t));
+// }
+ if mipM[i] == nil {
+ F.s("existence: mipM[").d(i).s("] == nil").putnl();
+ }
+ }
+
+ // test nonexistence with zero check
+ // failed lookups yield the 'zero' of the type.
+ // a later loop uses the more reliable tuple check.
+ for i := count; i < 2*count; i++ {
+ s := F.d(i).str();
+ f := float(i);
+ // BUG t := T(s, f);
+ var t T; t.s = s; t.f = f;
+ // BUG m := M(i, i+1);
+ if mib[i] != false {
+ F.s("nonexistence: mib[").d(i).s("] != false").putnl();
+ }
+ if mii[i] != 0 {
+ F.s("nonexistence: mii[").d(i).s("] != 0").putnl();
+ }
+ if mfi[f] != 0 {
+ F.s("nonexistence: mfi[").g(f).s("] != 0").putnl();
+ }
+ if mif[i] != 0.0 {
+ F.s("nonexistence: mif[").d(i).s("] != 0.0").putnl();
+ }
+ if mis[i] != `` {
+ F.s("nonexistence: mis[").d(i).s("] != ``").putnl();
+ }
+ if msi[s] != 0 {
+ F.s("nonexistence: msi[").s(s).s("] != 0").putnl();
+ }
+ if mss[s] != `` {
+ F.s("nonexistence: mss[").s(s).s("] != ``").putnl();
+ }
+ if mspa[s] != nil {
+ F.s("nonexistence: mspa[").s(s).s("] != ``").putnl();
+ }
+ if mipT[i] != nil {
+ F.s("nonexistence: mipT[").d(i).s("] != nil").putnl();
+ }
+ if mpTi[apT[i]] != 0 {
+ F.s("nonexistence: mpTi[apT[").d(i).s("]] != 0").putnl();
+ }
+// if mti[t] != 0 {
+// // emit stdout <- format("haskey mti[%s] false", string(t));
+//
+ if mipM[i] != nil {
+ F.s("nonexistence: mipM[").d(i).s("] != nil").putnl();
+ }
+ }
+
+ // test existence with tuple check
+ // failed lookups yield a false value for the boolean.
+ for i := 0; i < count; i++ {
+ s := F.d(i).str();
+ f := float(i);
+ // BUG t := T(s, f);
+ var t T; t.s = s; t.f = f;
+ // BUG m := M(i, i+1);
+ {
+ a, b := mib[i];
+ if !b {
+ F.s("tuple existence decl: mib[").d(i).s("]").putnl();
+ }
+ a, b = mib[i];
+ if !b {
+ F.s("tuple existence assign: mib[").d(i).s("]").putnl();
+ }
+ }
+ {
+ a, b := mii[i];
+ if !b {
+ F.s("tuple existence decl: mii[").d(i).s("]").putnl();
+ }
+ a, b = mii[i];
+ if !b {
+ F.s("tuple existence assign: mii[").d(i).s("]").putnl();
+ }
+ }
+ {
+ a, b := mfi[f];
+ if !b {
+ F.s("tuple existence decl: mfi[").g(f).s("]").putnl();
+ }
+ a, b = mfi[f];
+ if !b {
+ F.s("tuple existence assign: mfi[").g(f).s("]").putnl();
+ }
+ }
+ {
+ a, b := mif[i];
+ if !b {
+ F.s("tuple existence decl: mif[").d(i).s("]").putnl();
+ }
+ a, b = mif[i];
+ if !b {
+ F.s("tuple existence assign: mif[").d(i).s("]").putnl();
+ }
+ }
+ {
+ a, b := mis[i];
+ if !b {
+ F.s("tuple existence decl: mis[").d(i).s("]").putnl();
+ }
+ a, b = mis[i];
+ if !b {
+ F.s("tuple existence assign: mis[").d(i).s("]").putnl();
+ }
+ }
+ {
+ a, b := msi[s];
+ if !b {
+ F.s("tuple existence decl: msi[").s(s).s("]").putnl();
+ }
+ a, b = msi[s];
+ if !b {
+ F.s("tuple existence assign: msi[").s(s).s("]").putnl();
+ }
+ }
+ {
+ a, b := mss[s];
+ if !b {
+ F.s("tuple existence decl: mss[").s(s).s("]").putnl();
+ }
+ a, b = mss[s];
+ if !b {
+ F.s("tuple existence assign: mss[").s(s).s("]").putnl();
+ }
+ }
+ {
+ a, b := mspa[s];
+ if !b {
+ F.s("tuple existence decl: mspa[").s(s).s("]").putnl();
+ }
+ a, b = mspa[s];
+ if !b {
+ F.s("tuple existence assign: mspa[").s(s).s("]").putnl();
+ }
+ }
+ {
+ a, b := mipT[i];
+ if !b {
+ F.s("tuple existence decl: mipT[").d(i).s("]").putnl();
+ }
+ a, b = mipT[i];
+ if !b {
+ F.s("tuple existence assign: mipT[").d(i).s("]").putnl();
+ }
+ }
+ {
+ a, b := mpTi[apT[i]];
+ if !b {
+ F.s("tuple existence decl: mpTi[apT[").d(i).s("]]").putnl();
+ }
+ a, b = mpTi[apT[i]];
+ if !b {
+ F.s("tuple existence assign: mpTi[apT[").d(i).s("]]").putnl();
+ }
+ }
+// a,b := mti[t]...
+// emit stdout <- format("haskey mti[%s] false", string(t));
+ {
+ a, b := mipM[i];
+ if !b {
+ F.s("tuple existence decl: mipM[").d(i).s("]").putnl();
+ }
+ a, b = mipM[i];
+ if !b {
+ F.s("tuple existence assign: mipM[").d(i).s("]").putnl();
+ }
+ }
+ }
+
+ // test nonexistence with tuple check
+ // failed lookups yield a false value for the boolean.
+ for i := count; i < 2*count; i++ {
+ s := F.d(i).str();
+ f := float(i);
+ // BUG t := T(s, f);
+ var t T; t.s = s; t.f = f;
+ // BUG m := M(i, i+1);
+ {
+ a, b := mib[i];
+ if b {
+ F.s("tuple nonexistence decl: mib[").d(i).s("]").putnl();
+ }
+ a, b = mib[i];
+ if b {
+ F.s("tuple nonexistence assign: mib[").d(i).s("]").putnl();
+ }
+ }
+ {
+ a, b := mii[i];
+ if b {
+ F.s("tuple nonexistence decl: mii[").d(i).s("]").putnl();
+ }
+ a, b = mii[i];
+ if b {
+ F.s("tuple nonexistence assign: mii[").d(i).s("]").putnl();
+ }
+ }
+ {
+ a, b := mfi[f];
+ if b {
+ F.s("tuple nonexistence decl: mfi[").g(f).s("]").putnl();
+ }
+ a, b = mfi[f];
+ if b {
+ F.s("tuple nonexistence assign: mfi[").g(f).s("]").putnl();
+ }
+ }
+ {
+ a, b := mif[i];
+ if b {
+ F.s("tuple nonexistence decl: mif[").d(i).s("]").putnl();
+ }
+ a, b = mif[i];
+ if b {
+ F.s("tuple nonexistence assign: mif[").d(i).s("]").putnl();
+ }
+ }
+ {
+ a, b := mis[i];
+ if b {
+ F.s("tuple nonexistence decl: mis[").d(i).s("]").putnl();
+ }
+ a, b = mis[i];
+ if b {
+ F.s("tuple nonexistence assign: mis[").d(i).s("]").putnl();
+ }
+ }
+ {
+ a, b := msi[s];
+ if b {
+ F.s("tuple nonexistence decl: msi[").s(s).s("]").putnl();
+ }
+ a, b = msi[s];
+ if b {
+ F.s("tuple nonexistence assign: msi[").s(s).s("]").putnl();
+ }
+ }
+ {
+ a, b := mss[s];
+ if b {
+ F.s("tuple nonexistence decl: mss[").s(s).s("]").putnl();
+ }
+ a, b = mss[s];
+ if b {
+ F.s("tuple nonexistence assign: mss[").s(s).s("]").putnl();
+ }
+ }
+ {
+ a, b := mspa[s];
+ if b {
+ F.s("tuple nonexistence decl: mspa[").s(s).s("]").putnl();
+ }
+ a, b = mspa[s];
+ if b {
+ F.s("tuple nonexistence assign: mspa[").s(s).s("]").putnl();
+ }
+ }
+ {
+ a, b := mipT[i];
+ if b {
+ F.s("tuple nonexistence decl: mipT[").d(i).s("]").putnl();
+ }
+ a, b = mipT[i];
+ if b {
+ F.s("tuple nonexistence assign: mipT[").d(i).s("]").putnl();
+ }
+ }
+ {
+ a, b := mpTi[apT[i]];
+ if b {
+ F.s("tuple nonexistence decl: mpTi[apt[").d(i).s("]]").putnl();
+ }
+ a, b = mpTi[apT[i]];
+ if b {
+ F.s("tuple nonexistence assign: mpTi[apT[").d(i).s("]]").putnl();
+ }
+ }
+// a,b := mti[t]...
+// emit stdout <- format("haskey mti[%s] false", string(t));
+ {
+ a, b := mipM[i];
+ if b {
+ F.s("tuple nonexistence decl: mipM[").d(i).s("]").putnl();
+ }
+ a, b = mipM[i];
+ if b {
+ F.s("tuple nonexistence assign: mipM[").d(i).s("]").putnl();
+ }
+ }
+ }
+
+ // tests for structured map element updates
+ for i := 0; i < count; i++ {
+ s := F.d(i).str();
+ mspa[s][i % 2] = "deleted";
+ if mspa[s][i % 2] != "deleted" {
+ F.s("mspa[").d(i).s("][").d(i).s("%2] =").s(mspa[s][i % 2]).putnl();
+ }
+ mipT[i].s = string('a' + i % 26) + mipT[i].s[1:len(s)];
+ first := string('a' + i % 26);
+ if mipT[i].s != first + s[1:len(s)] {
+ F.s("mit[").d(i).s("].s = ").s(mipT[i].s).putnl();
+ }
+ mipT[i].f = float(i + 1);
+ if (mipT[i].f != float(i + 1)) {
+ F.s("mipT[").d(i).s("].f = ").g(mipT[i].f).putnl();
+ }
+ mipM[i][i]++;
+ if mipM[i][i] != (i + 1) + 1 {
+ F.s("mipM[").d(i).s("][").d(i).s("] = ").d(mipM[i][i]).putnl();
+ }
+ }
+}