Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / expvar / expvar_test.go
blob3dfc55af36406b18f1cad8580433491c2b9c0023
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 "json"
9 "testing"
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)
17 if reqs != Get("requests").(*Int) {
18 t.Errorf("Get() failed.")
21 reqs.Add(1)
22 reqs.Add(3)
23 if reqs.i != 4 {
24 t.Errorf("reqs.i = %v, want 4", reqs.i)
27 if s := reqs.String(); s != "4" {
28 t.Errorf("reqs.String() = %q, want \"4\"", s)
31 reqs.Set(-2)
32 if reqs.i != -2 {
33 t.Errorf("reqs.i = %v, want -2", reqs.i)
37 func TestString(t *testing.T) {
38 name := NewString("my-name")
39 if name.s != "" {
40 t.Errorf("name.s = %q, want \"\"", name.s)
43 name.Set("Mike")
44 if name.s != "Mike" {
45 t.Errorf("name.s = %q, want \"Mike\"", name.s)
48 if s := name.String(); s != "\"Mike\"" {
49 t.Errorf("reqs.String() = %q, want \"\"Mike\"\"", s)
53 func TestMapCounter(t *testing.T) {
54 colours := NewMap("bike-shed-colours")
56 colours.Add("red", 1)
57 colours.Add("red", 2)
58 colours.Add("blue", 4)
59 if x := colours.m["red"].(*Int).i; x != 3 {
60 t.Errorf("colours.m[\"red\"] = %v, want 3", x)
62 if x := colours.m["blue"].(*Int).i; x != 4 {
63 t.Errorf("colours.m[\"blue\"] = %v, want 4", x)
66 // colours.String() should be '{"red":3, "blue":4}',
67 // though the order of red and blue could vary.
68 s := colours.String()
69 var j interface{}
70 err := json.Unmarshal([]byte(s), &j)
71 if err != nil {
72 t.Errorf("colours.String() isn't valid JSON: %v", err)
74 m, ok := j.(map[string]interface{})
75 if !ok {
76 t.Error("colours.String() didn't produce a map.")
78 red := m["red"]
79 x, ok := red.(float64)
80 if !ok {
81 t.Error("red.Kind() is not a number.")
83 if x != 3 {
84 t.Errorf("red = %v, want 3", x)
88 func TestIntFunc(t *testing.T) {
89 x := int(4)
90 ix := IntFunc(func() int64 { return int64(x) })
91 if s := ix.String(); s != "4" {
92 t.Errorf("ix.String() = %v, want 4", s)
95 x++
96 if s := ix.String(); s != "5" {
97 t.Errorf("ix.String() = %v, want 5", s)