OSDN Git Service

PR go/48242
[pf3gnuchains/gcc-fork.git] / libgo / go / expvar / expvar_test.go
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.
4
5 package expvar
6
7 import (
8         "json"
9         "testing"
10 )
11
12 func TestInt(t *testing.T) {
13         reqs := NewInt("requests")
14         if reqs.i != 0 {
15                 t.Errorf("reqs.i = %v, want 0", reqs.i)
16         }
17         if reqs != Get("requests").(*Int) {
18                 t.Errorf("Get() failed.")
19         }
20
21         reqs.Add(1)
22         reqs.Add(3)
23         if reqs.i != 4 {
24                 t.Errorf("reqs.i = %v, want 4", reqs.i)
25         }
26
27         if s := reqs.String(); s != "4" {
28                 t.Errorf("reqs.String() = %q, want \"4\"", s)
29         }
30
31         reqs.Set(-2)
32         if reqs.i != -2 {
33                 t.Errorf("reqs.i = %v, want -2", reqs.i)
34         }
35 }
36
37 func TestFloat(t *testing.T) {
38         reqs := NewFloat("requests-float")
39         if reqs.f != 0.0 {
40                 t.Errorf("reqs.f = %v, want 0", reqs.f)
41         }
42         if reqs != Get("requests-float").(*Float) {
43                 t.Errorf("Get() failed.")
44         }
45
46         reqs.Add(1.5)
47         reqs.Add(1.25)
48         if reqs.f != 2.75 {
49                 t.Errorf("reqs.f = %v, want 2.75", reqs.f)
50         }
51
52         if s := reqs.String(); s != "2.75" {
53                 t.Errorf("reqs.String() = %q, want \"4.64\"", s)
54         }
55
56         reqs.Add(-2)
57         if reqs.f != 0.75 {
58                 t.Errorf("reqs.f = %v, want 0.75", reqs.f)
59         }
60 }
61
62 func TestString(t *testing.T) {
63         name := NewString("my-name")
64         if name.s != "" {
65                 t.Errorf("name.s = %q, want \"\"", name.s)
66         }
67
68         name.Set("Mike")
69         if name.s != "Mike" {
70                 t.Errorf("name.s = %q, want \"Mike\"", name.s)
71         }
72
73         if s := name.String(); s != "\"Mike\"" {
74                 t.Errorf("reqs.String() = %q, want \"\"Mike\"\"", s)
75         }
76 }
77
78 func TestMapCounter(t *testing.T) {
79         colours := NewMap("bike-shed-colours")
80
81         colours.Add("red", 1)
82         colours.Add("red", 2)
83         colours.Add("blue", 4)
84         colours.AddFloat("green", 4.125)
85         if x := colours.m["red"].(*Int).i; x != 3 {
86                 t.Errorf("colours.m[\"red\"] = %v, want 3", x)
87         }
88         if x := colours.m["blue"].(*Int).i; x != 4 {
89                 t.Errorf("colours.m[\"blue\"] = %v, want 4", x)
90         }
91         if x := colours.m["green"].(*Float).f; x != 4.125 {
92                 t.Errorf("colours.m[\"green\"] = %v, want 3.14", x)
93         }
94
95         // colours.String() should be '{"red":3, "blue":4}',
96         // though the order of red and blue could vary.
97         s := colours.String()
98         var j interface{}
99         err := json.Unmarshal([]byte(s), &j)
100         if err != nil {
101                 t.Errorf("colours.String() isn't valid JSON: %v", err)
102         }
103         m, ok := j.(map[string]interface{})
104         if !ok {
105                 t.Error("colours.String() didn't produce a map.")
106         }
107         red := m["red"]
108         x, ok := red.(float64)
109         if !ok {
110                 t.Error("red.Kind() is not a number.")
111         }
112         if x != 3 {
113                 t.Errorf("red = %v, want 3", x)
114         }
115 }
116
117 func TestIntFunc(t *testing.T) {
118         x := int64(4)
119         ix := IntFunc(func() int64 { return x })
120         if s := ix.String(); s != "4" {
121                 t.Errorf("ix.String() = %v, want 4", s)
122         }
123
124         x++
125         if s := ix.String(); s != "5" {
126                 t.Errorf("ix.String() = %v, want 5", s)
127         }
128 }
129
130 func TestFloatFunc(t *testing.T) {
131         x := 8.5
132         ix := FloatFunc(func() float64 { return x })
133         if s := ix.String(); s != "8.5" {
134                 t.Errorf("ix.String() = %v, want 3.14", s)
135         }
136
137         x -= 1.25
138         if s := ix.String(); s != "7.25" {
139                 t.Errorf("ix.String() = %v, want 4.34", s)
140         }
141 }
142
143 func TestStringFunc(t *testing.T) {
144         x := "hello"
145         sx := StringFunc(func() string { return x })
146         if s, exp := sx.String(), `"hello"`; s != exp {
147                 t.Errorf(`sx.String() = %q, want %q`, s, exp)
148         }
149
150         x = "goodbye"
151         if s, exp := sx.String(), `"goodbye"`; s != exp {
152                 t.Errorf(`sx.String() = %q, want %q`, s, exp)
153         }
154 }