Update to current Go library.
[official-gcc.git] / libgo / go / container / vector / numbers_test.go
blobb83b0bfeeff319cd1e4f2b6248fde4482ed8d1bd
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package vector
7 import (
8 "fmt"
9 "runtime"
10 "strings"
11 "testing"
15 const memTestN = 1000000
18 func s(n uint64) string {
19 str := fmt.Sprintf("%d", n)
20 lens := len(str)
21 a := make([]string, (lens+2)/3)
22 start := lens
23 for i := range a {
24 start -= 3
25 if start < 0 {
26 start = 0
28 a[len(a)-i-1] = str[start:lens]
29 lens -= 3
31 return strings.Join(a, " ")
35 func TestVectorNums(t *testing.T) {
36 if testing.Short() {
37 return
39 var v Vector
40 c := int(0)
41 runtime.GC()
42 m0 := runtime.MemStats
43 v.Resize(memTestN, memTestN)
44 for i := 0; i < memTestN; i++ {
45 v.Set(i, c)
47 runtime.GC()
48 m := runtime.MemStats
49 v.Resize(0, 0)
50 runtime.GC()
51 n := m.Alloc - m0.Alloc
52 t.Logf("%T.Push(%#v), n = %s: Alloc/n = %.2f\n", v, c, s(memTestN), float64(n)/memTestN)
56 func TestIntVectorNums(t *testing.T) {
57 if testing.Short() {
58 return
60 var v IntVector
61 c := int(0)
62 runtime.GC()
63 m0 := runtime.MemStats
64 v.Resize(memTestN, memTestN)
65 for i := 0; i < memTestN; i++ {
66 v.Set(i, c)
68 runtime.GC()
69 m := runtime.MemStats
70 v.Resize(0, 0)
71 runtime.GC()
72 n := m.Alloc - m0.Alloc
73 t.Logf("%T.Push(%#v), n = %s: Alloc/n = %.2f\n", v, c, s(memTestN), float64(n)/memTestN)
77 func TestStringVectorNums(t *testing.T) {
78 if testing.Short() {
79 return
81 var v StringVector
82 c := ""
83 runtime.GC()
84 m0 := runtime.MemStats
85 v.Resize(memTestN, memTestN)
86 for i := 0; i < memTestN; i++ {
87 v.Set(i, c)
89 runtime.GC()
90 m := runtime.MemStats
91 v.Resize(0, 0)
92 runtime.GC()
93 n := m.Alloc - m0.Alloc
94 t.Logf("%T.Push(%#v), n = %s: Alloc/n = %.2f\n", v, c, s(memTestN), float64(n)/memTestN)
98 func BenchmarkVectorNums(b *testing.B) {
99 c := int(0)
100 var v Vector
101 b.StopTimer()
102 runtime.GC()
103 b.StartTimer()
104 for i := 0; i < b.N; i++ {
105 v.Push(c)
110 func BenchmarkIntVectorNums(b *testing.B) {
111 c := int(0)
112 var v IntVector
113 b.StopTimer()
114 runtime.GC()
115 b.StartTimer()
116 for i := 0; i < b.N; i++ {
117 v.Push(c)
122 func BenchmarkStringVectorNums(b *testing.B) {
123 c := ""
124 var v StringVector
125 b.StopTimer()
126 runtime.GC()
127 b.StartTimer()
128 for i := 0; i < b.N; i++ {
129 v.Push(c)