2014-04-11 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / libgo / go / expvar / expvar_test.go
blob572c62beed56cb3eb434ff8ebc124e3512c7822b
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 expvar
7 import (
8 "encoding/json"
9 "testing"
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)
20 func TestInt(t *testing.T) {
21 RemoveAll()
22 reqs := NewInt("requests")
23 if reqs.i != 0 {
24 t.Errorf("reqs.i = %v, want 0", reqs.i)
26 if reqs != Get("requests").(*Int) {
27 t.Errorf("Get() failed.")
30 reqs.Add(1)
31 reqs.Add(3)
32 if reqs.i != 4 {
33 t.Errorf("reqs.i = %v, want 4", reqs.i)
36 if s := reqs.String(); s != "4" {
37 t.Errorf("reqs.String() = %q, want \"4\"", s)
40 reqs.Set(-2)
41 if reqs.i != -2 {
42 t.Errorf("reqs.i = %v, want -2", reqs.i)
46 func TestFloat(t *testing.T) {
47 RemoveAll()
48 reqs := NewFloat("requests-float")
49 if reqs.f != 0.0 {
50 t.Errorf("reqs.f = %v, want 0", reqs.f)
52 if reqs != Get("requests-float").(*Float) {
53 t.Errorf("Get() failed.")
56 reqs.Add(1.5)
57 reqs.Add(1.25)
58 if reqs.f != 2.75 {
59 t.Errorf("reqs.f = %v, want 2.75", reqs.f)
62 if s := reqs.String(); s != "2.75" {
63 t.Errorf("reqs.String() = %q, want \"4.64\"", s)
66 reqs.Add(-2)
67 if reqs.f != 0.75 {
68 t.Errorf("reqs.f = %v, want 0.75", reqs.f)
72 func TestString(t *testing.T) {
73 RemoveAll()
74 name := NewString("my-name")
75 if name.s != "" {
76 t.Errorf("name.s = %q, want \"\"", name.s)
79 name.Set("Mike")
80 if name.s != "Mike" {
81 t.Errorf("name.s = %q, want \"Mike\"", name.s)
84 if s := name.String(); s != "\"Mike\"" {
85 t.Errorf("reqs.String() = %q, want \"\"Mike\"\"", s)
89 func TestMapCounter(t *testing.T) {
90 RemoveAll()
91 colors := NewMap("bike-shed-colors")
93 colors.Add("red", 1)
94 colors.Add("red", 2)
95 colors.Add("blue", 4)
96 colors.AddFloat("green", 4.125)
97 if x := colors.m["red"].(*Int).i; x != 3 {
98 t.Errorf("colors.m[\"red\"] = %v, want 3", x)
100 if x := colors.m["blue"].(*Int).i; x != 4 {
101 t.Errorf("colors.m[\"blue\"] = %v, want 4", x)
103 if x := colors.m["green"].(*Float).f; x != 4.125 {
104 t.Errorf("colors.m[\"green\"] = %v, want 3.14", x)
107 // colors.String() should be '{"red":3, "blue":4}',
108 // though the order of red and blue could vary.
109 s := colors.String()
110 var j interface{}
111 err := json.Unmarshal([]byte(s), &j)
112 if err != nil {
113 t.Errorf("colors.String() isn't valid JSON: %v", err)
115 m, ok := j.(map[string]interface{})
116 if !ok {
117 t.Error("colors.String() didn't produce a map.")
119 red := m["red"]
120 x, ok := red.(float64)
121 if !ok {
122 t.Error("red.Kind() is not a number.")
124 if x != 3 {
125 t.Errorf("red = %v, want 3", x)
129 func TestFunc(t *testing.T) {
130 RemoveAll()
131 var x interface{} = []string{"a", "b"}
132 f := Func(func() interface{} { return x })
133 if s, exp := f.String(), `["a","b"]`; s != exp {
134 t.Errorf(`f.String() = %q, want %q`, s, exp)
137 x = 17
138 if s, exp := f.String(), `17`; s != exp {
139 t.Errorf(`f.String() = %q, want %q`, s, exp)