OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [expvar/] [expvar_test.go] - Blame information for rev 861

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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.
4
 
5
package expvar
6
 
7
import (
8
        "encoding/json"
9
        "testing"
10
)
11
 
12
// RemoveAll removes all exported variables.
13
// This is for tests only.
14
func RemoveAll() {
15
        mutex.Lock()
16
        defer mutex.Unlock()
17
        vars = make(map[string]Var)
18
}
19
 
20
func TestInt(t *testing.T) {
21
        reqs := NewInt("requests")
22
        if reqs.i != 0 {
23
                t.Errorf("reqs.i = %v, want 0", reqs.i)
24
        }
25
        if reqs != Get("requests").(*Int) {
26
                t.Errorf("Get() failed.")
27
        }
28
 
29
        reqs.Add(1)
30
        reqs.Add(3)
31
        if reqs.i != 4 {
32
                t.Errorf("reqs.i = %v, want 4", reqs.i)
33
        }
34
 
35
        if s := reqs.String(); s != "4" {
36
                t.Errorf("reqs.String() = %q, want \"4\"", s)
37
        }
38
 
39
        reqs.Set(-2)
40
        if reqs.i != -2 {
41
                t.Errorf("reqs.i = %v, want -2", reqs.i)
42
        }
43
}
44
 
45
func TestFloat(t *testing.T) {
46
        reqs := NewFloat("requests-float")
47
        if reqs.f != 0.0 {
48
                t.Errorf("reqs.f = %v, want 0", reqs.f)
49
        }
50
        if reqs != Get("requests-float").(*Float) {
51
                t.Errorf("Get() failed.")
52
        }
53
 
54
        reqs.Add(1.5)
55
        reqs.Add(1.25)
56
        if reqs.f != 2.75 {
57
                t.Errorf("reqs.f = %v, want 2.75", reqs.f)
58
        }
59
 
60
        if s := reqs.String(); s != "2.75" {
61
                t.Errorf("reqs.String() = %q, want \"4.64\"", s)
62
        }
63
 
64
        reqs.Add(-2)
65
        if reqs.f != 0.75 {
66
                t.Errorf("reqs.f = %v, want 0.75", reqs.f)
67
        }
68
}
69
 
70
func TestString(t *testing.T) {
71
        name := NewString("my-name")
72
        if name.s != "" {
73
                t.Errorf("name.s = %q, want \"\"", name.s)
74
        }
75
 
76
        name.Set("Mike")
77
        if name.s != "Mike" {
78
                t.Errorf("name.s = %q, want \"Mike\"", name.s)
79
        }
80
 
81
        if s := name.String(); s != "\"Mike\"" {
82
                t.Errorf("reqs.String() = %q, want \"\"Mike\"\"", s)
83
        }
84
}
85
 
86
func TestMapCounter(t *testing.T) {
87
        colors := NewMap("bike-shed-colors")
88
 
89
        colors.Add("red", 1)
90
        colors.Add("red", 2)
91
        colors.Add("blue", 4)
92
        colors.AddFloat("green", 4.125)
93
        if x := colors.m["red"].(*Int).i; x != 3 {
94
                t.Errorf("colors.m[\"red\"] = %v, want 3", x)
95
        }
96
        if x := colors.m["blue"].(*Int).i; x != 4 {
97
                t.Errorf("colors.m[\"blue\"] = %v, want 4", x)
98
        }
99
        if x := colors.m["green"].(*Float).f; x != 4.125 {
100
                t.Errorf("colors.m[\"green\"] = %v, want 3.14", x)
101
        }
102
 
103
        // colors.String() should be '{"red":3, "blue":4}',
104
        // though the order of red and blue could vary.
105
        s := colors.String()
106
        var j interface{}
107
        err := json.Unmarshal([]byte(s), &j)
108
        if err != nil {
109
                t.Errorf("colors.String() isn't valid JSON: %v", err)
110
        }
111
        m, ok := j.(map[string]interface{})
112
        if !ok {
113
                t.Error("colors.String() didn't produce a map.")
114
        }
115
        red := m["red"]
116
        x, ok := red.(float64)
117
        if !ok {
118
                t.Error("red.Kind() is not a number.")
119
        }
120
        if x != 3 {
121
                t.Errorf("red = %v, want 3", x)
122
        }
123
}
124
 
125
func TestFunc(t *testing.T) {
126
        var x interface{} = []string{"a", "b"}
127
        f := Func(func() interface{} { return x })
128
        if s, exp := f.String(), `["a","b"]`; s != exp {
129
                t.Errorf(`f.String() = %q, want %q`, s, exp)
130
        }
131
 
132
        x = 17
133
        if s, exp := f.String(), `17`; s != exp {
134
                t.Errorf(`f.String() = %q, want %q`, s, exp)
135
        }
136
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.