summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2008-03-26 09:28:57 -0700
committerRobert Griesemer <gri@golang.org>2008-03-26 09:28:57 -0700
commit6cbb50ac6dca6176685ba9b6e165e691f00cb47c (patch)
tree34456e7f25b74ab46ae7f6fe3b123b9155edfc87 /test
parent37e52258ee5067e2d6864435a7281b19b403764d (diff)
downloadgolang-6cbb50ac6dca6176685ba9b6e165e691f00cb47c.tar.gz
- new directory structure
SVN=113851
Diffstat (limited to 'test')
-rw-r--r--test/char_lit.go35
-rw-r--r--test/float_lit.go70
-rwxr-xr-xtest/hashmap.go186
-rw-r--r--test/int_lit.go19
-rwxr-xr-xtest/runtests.sh8
-rw-r--r--test/sieve.go42
-rw-r--r--test/string_lit.go29
-rw-r--r--test/test0.go81
8 files changed, 470 insertions, 0 deletions
diff --git a/test/char_lit.go b/test/char_lit.go
new file mode 100644
index 000000000..66ffec208
--- /dev/null
+++ b/test/char_lit.go
@@ -0,0 +1,35 @@
+// $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
+
+func main() {
+ [ ' ',
+ 'a',
+ // need to fix Coco/R scanner to read Unicode.
+ // 'ä',
+ //'本',
+ '\a',
+ '\b',
+ '\f',
+ '\n',
+ '\r',
+ '\t',
+ '\v',
+ '\\',
+ '\'',
+ '\"',
+ '\000',
+ '\123',
+ '\x00',
+ '\xca',
+ '\xFE',
+ '\u0123',
+ '\ubabe',
+ '\U0123ABCD',
+ '\Ucafebabe'
+ ]
+}
diff --git a/test/float_lit.go b/test/float_lit.go
new file mode 100644
index 000000000..11decaffb
--- /dev/null
+++ b/test/float_lit.go
@@ -0,0 +1,70 @@
+// $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
+
+func main() {
+ [ 0.,
+ +10.,
+ -210.,
+
+ .0,
+ +.01,
+ -.012,
+
+ 0.0,
+ +10.01,
+ -210.012,
+
+ 0E+1,
+ +10e2,
+ -210e3,
+
+ 0E-1,
+ +0e23,
+ -0e345,
+
+ 0E1,
+ +10e23,
+ -210e345,
+
+ 0.E1,
+ +10.e+2,
+ -210.e-3,
+
+ .0E1,
+ +.01e2,
+ -.012e3,
+
+ 0.0E1,
+ +10.01e2,
+ -210.012e3,
+
+ 0.E+12,
+ +10.e23,
+ -210.e34,
+
+ .0E-12,
+ +.01e23,
+ -.012e34,
+
+ 0.0E12,
+ +10.01e23,
+ -210.012e34,
+
+ 0.E123,
+ +10.e+234,
+ -210.e-345,
+
+ .0E123,
+ +.01e234,
+ -.012e345,
+
+ 0.0E123,
+ +10.01e234,
+ -210.012e345
+ ]
+}
diff --git a/test/hashmap.go b/test/hashmap.go
new file mode 100755
index 000000000..daa0d4374
--- /dev/null
+++ b/test/hashmap.go
@@ -0,0 +1,186 @@
+// $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.
+
+// To compile: go hashmap.go && gcc -o g.out main.go.c gort0.c && g.out
+
+package main
+
+// ----------------------------------------------------------------------------
+// Helper functions
+
+func ASSERT(p bool) {
+ if !p {
+ // panic 0;
+ }
+}
+
+
+// ----------------------------------------------------------------------------
+// Implementation of the HashMap
+
+type KeyType interface {
+ Hash() uint32;
+ Match(other *KeyType) bool
+}
+
+
+type ValueType interface {
+ // empty interface
+}
+
+
+type Entry struct {
+ key *KeyType;
+ value *ValueType;
+}
+
+
+// Using the Array type below doesn't seem to work
+//type Array array [1024] Entry;
+
+type HashMap struct {
+ map_ *[1024] Entry;
+ log2_capacity_ uint32;
+ occupancy_ uint32;
+}
+
+
+func (m *HashMap) capacity() uint32 {
+ return 1 << m.log2_capacity_;
+}
+
+
+func (m *HashMap) Clear() {
+ // Mark all entries as empty.
+ var i uint32 = m.capacity() - 1;
+ for i > 0 {
+ m.map_[i].key = nil;
+ i = i - 1
+ }
+ m.occupancy_ = 0
+}
+
+
+func (m *HashMap) Initialize (initial_log2_capacity uint32) {
+ m.log2_capacity_ = initial_log2_capacity;
+ m.map_ = new([1024] Entry);
+ m.Clear();
+}
+
+
+func (m *HashMap) Probe (key *KeyType) *Entry {
+ ASSERT(key != nil);
+
+ var i uint32 = key.Hash() % m.capacity();
+ ASSERT(0 <= i && i < m.capacity());
+
+ ASSERT(m.occupancy_ < m.capacity()); // guarantees loop termination
+ for m.map_[i].key != nil && !m.map_[i].key.Match(key) {
+ i++;
+ if i >= m.capacity() {
+ i = 0;
+ }
+ }
+
+ return &m.map_[i];
+}
+
+
+func (m *HashMap) Resize();
+
+
+func (m *HashMap) Lookup (key *KeyType, insert bool) *Entry {
+ // Find a matching entry.
+ var p *Entry = m.Probe(key);
+ if p.key != nil {
+ return p;
+ }
+
+ // No entry found; insert one if necessary.
+ if insert {
+ p.key = key;
+ p.value = nil;
+ m.occupancy_++;
+
+ // Grow the map if we reached >= 80% occupancy.
+ if m.occupancy_ + m.occupancy_/4 >= m.capacity() {
+ m.Resize();
+ p = m.Probe(key);
+ }
+
+ return p;
+ }
+
+ // No entry found and none inserted.
+ return nil;
+}
+
+
+func (m *HashMap) Resize() {
+ var hmap *[1024] Entry = m.map_;
+ var n uint32 = m.occupancy_;
+
+ // Allocate a new map of twice the current size.
+ m.Initialize(m.log2_capacity_ << 1);
+
+ // Rehash all current entries.
+ var i uint32 = 0;
+ for n > 0 {
+ if hmap[i].key != nil {
+ m.Lookup(hmap[i].key, true).value = hmap[i].value;
+ n = n - 1;
+ }
+ i++;
+ }
+}
+
+
+// ----------------------------------------------------------------------------
+// Test code
+
+type Number struct {
+ x uint32;
+}
+
+
+func (n *Number) Hash() uint32 {
+ return n.x * 23;
+}
+
+
+func (n *Number) Match(other *KeyType) bool {
+ // var y *Number = other;
+ // return n.x == y.x;
+ return false;
+}
+
+
+func MakeNumber (x uint32) *Number {
+ var n *Number = new(Number);
+ n.x = x;
+ return n;
+}
+
+
+func main() {
+ //f unc (n int) int { return n + 1; }(1);
+
+ //print "HashMap - gri 2/8/2008\n";
+
+ var hmap *HashMap = new(HashMap);
+ hmap.Initialize(0);
+
+ var x1 *Number = MakeNumber(1001);
+ var x2 *Number = MakeNumber(2002);
+ var x3 *Number = MakeNumber(3003);
+
+ // this doesn't work I think...
+ //hmap.Lookup(x1, true);
+ //hmap.Lookup(x2, true);
+ //hmap.Lookup(x3, true);
+
+ //print "done\n";
+}
diff --git a/test/int_lit.go b/test/int_lit.go
new file mode 100644
index 000000000..ef74370ac
--- /dev/null
+++ b/test/int_lit.go
@@ -0,0 +1,19 @@
+// $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
+
+func main() {
+ [ 0,
+ 123,
+ 0123,
+ 0000,
+ 0x0,
+ 0x123,
+ 0X0,
+ 0X123
+ ];
+}
diff --git a/test/runtests.sh b/test/runtests.sh
new file mode 100755
index 000000000..75b7ae26a
--- /dev/null
+++ b/test/runtests.sh
@@ -0,0 +1,8 @@
+# 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.
+
+#!/bin/bash
+for f in *.go; do
+ ../src/go $f
+done
diff --git a/test/sieve.go b/test/sieve.go
new file mode 100644
index 000000000..03538d62a
--- /dev/null
+++ b/test/sieve.go
@@ -0,0 +1,42 @@
+// $G $F.go && $L $F.$A # don't run it - goes forever
+
+// 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
+
+// Send the sequence 2, 3, 4, ... to channel 'ch'.
+func Generate(ch *chan> int) {
+ for i := 2; ; i++ {
+ >ch = i // Send 'i' to channel 'ch'.
+ }
+}
+
+// Copy the values from channel 'in' to channel 'out',
+// removing those divisible by 'prime'.
+func Filter(in *chan< int, out *chan> int, prime int) {
+ for {
+ i := <in; // Receive value of new variable 'i' from 'in'.
+ if i % prime != 0 {
+ >out = i // Send 'i' to channel 'out'.
+ }
+ }
+}
+
+// The prime sieve: Daisy-chain Filter processes together.
+func Sieve() {
+ ch := new(chan int); // Create a new channel.
+ go Generate(ch); // Start Generate() as a subprocess.
+ for {
+ prime := <ch;
+ printf("%d\n", prime);
+ ch1 := new(chan int);
+ go Filter(ch, ch1, prime);
+ ch = ch1
+ }
+}
+
+func Main() {
+ Sieve()
+}
diff --git a/test/string_lit.go b/test/string_lit.go
new file mode 100644
index 000000000..568e7a511
--- /dev/null
+++ b/test/string_lit.go
@@ -0,0 +1,29 @@
+// $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
+
+func main() {
+ [ "",
+ " ",
+ "'`",
+ "a",
+ //"ä",
+ //"本",
+ "\a\b\f\n\r\t\v\\\'\"",
+ "\000\123\x00\xca\xFE\u0123\ubabe\U0123ABCD\Ucafebabe",
+
+ ``,
+ ` `,
+ `'"`,
+ `a`,
+ //`ä`,
+ //`本`,
+ `\a\b\f\n\r\t\v\\\'\"`,
+ `\000\123\x00\xca\xFE\u0123\ubabe\U0123ABCD\Ucafebabe`,
+ `\x\u\U\`
+ ]
+}
diff --git a/test/test0.go b/test/test0.go
new file mode 100644
index 000000000..0d9585ed6
--- /dev/null
+++ b/test/test0.go
@@ -0,0 +1,81 @@
+// $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.
+
+// This is test0.go.
+
+package Test0
+
+const
+ a_const = 0
+
+const (
+ pi = /* the usual */ 3.14159265358979323;
+ e = 2.718281828;
+ mask1 int = 1 << iota;
+ mask2 = 1 << iota;
+ mask3 = 1 << iota;
+ mask4 = 1 << iota;
+)
+
+type (
+ Empty interface {};
+ Point struct {
+ x, y int;
+ };
+ Point2 Point
+)
+
+func (p *Point) Initialize(x, y int) {
+ p.x, p.y = x, y
+}
+
+func (p *Point) Distance() int {
+ return p.x * p.x + p.y * p.y
+}
+
+var (
+ x1 int;
+ x2 int;
+ u, v, w float
+)
+
+func foo() {}
+
+func min(x, y int) int {
+ if x < y { return x }
+ return y
+}
+
+func swap(x, y int) (u, v int) {
+ u = y;
+ v = x;
+ return
+}
+
+func control_structs() {
+ var p Point = new(Point).Initialize(2, 3);
+ i := p.Distance();
+ var f float = 0.3;
+ for {}
+ for {};
+ for j := 0; j < i; j++ {
+ if i == 0 {
+ } else i = 0;
+ var x float
+ }
+ foo: // a label
+ switch {
+ case i < y:
+ fallthrough;
+ case i < j:
+ case i == 0, i == 1, i == j:
+ i++; i++;
+ goto foo;
+ default:
+ i = -+-+i;
+ break
+ }
+}