blob: 0ca3f522d622aa7d6b2af6a97f253fa3eca869b5 [file] [log] [blame]
Ian Lance Taylor115066f2010-02-15 22:42:49 -08001// Copyright 2010 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
5package fmt_test
6
7import (
8 . "fmt"
9 "testing"
10)
11
12type TI int
13type TI8 int8
14type TI16 int16
15type TI32 int32
16type TI64 int64
17type TU uint
18type TU8 uint8
19type TU16 uint16
20type TU32 uint32
21type TU64 uint64
22type TUI uintptr
Russ Coxf2b5a072011-01-19 23:09:00 -050023type TF float64
Ian Lance Taylor115066f2010-02-15 22:42:49 -080024type TF32 float32
25type TF64 float64
26type TB bool
27type TS string
28
Rob Piked482c162010-06-14 17:16:35 -070029func (v TI) String() string { return Sprintf("I: %d", int(v)) }
30func (v TI8) String() string { return Sprintf("I8: %d", int8(v)) }
31func (v TI16) String() string { return Sprintf("I16: %d", int16(v)) }
32func (v TI32) String() string { return Sprintf("I32: %d", int32(v)) }
33func (v TI64) String() string { return Sprintf("I64: %d", int64(v)) }
34func (v TU) String() string { return Sprintf("U: %d", uint(v)) }
35func (v TU8) String() string { return Sprintf("U8: %d", uint8(v)) }
36func (v TU16) String() string { return Sprintf("U16: %d", uint16(v)) }
37func (v TU32) String() string { return Sprintf("U32: %d", uint32(v)) }
38func (v TU64) String() string { return Sprintf("U64: %d", uint64(v)) }
39func (v TUI) String() string { return Sprintf("UI: %d", uintptr(v)) }
Russ Coxf2b5a072011-01-19 23:09:00 -050040func (v TF) String() string { return Sprintf("F: %f", float64(v)) }
Rob Piked482c162010-06-14 17:16:35 -070041func (v TF32) String() string { return Sprintf("F32: %f", float32(v)) }
42func (v TF64) String() string { return Sprintf("F64: %f", float64(v)) }
43func (v TB) String() string { return Sprintf("B: %t", bool(v)) }
Rob Pike24ee7f72010-02-25 17:29:37 +110044func (v TS) String() string { return Sprintf("S: %q", string(v)) }
Ian Lance Taylor115066f2010-02-15 22:42:49 -080045
46func check(t *testing.T, got, want string) {
47 if got != want {
48 t.Error(got, "!=", want)
49 }
50}
51
52func TestStringer(t *testing.T) {
53 s := Sprintf("%v %v %v %v %v", TI(0), TI8(1), TI16(2), TI32(3), TI64(4))
54 check(t, s, "I: 0 I8: 1 I16: 2 I32: 3 I64: 4")
55 s = Sprintf("%v %v %v %v %v %v", TU(5), TU8(6), TU16(7), TU32(8), TU64(9), TUI(10))
56 check(t, s, "U: 5 U8: 6 U16: 7 U32: 8 U64: 9 UI: 10")
57 s = Sprintf("%v %v %v", TF(1.0), TF32(2.0), TF64(3.0))
58 check(t, s, "F: 1.000000 F32: 2.000000 F64: 3.000000")
59 s = Sprintf("%v %v", TB(true), TS("x"))
60 check(t, s, "B: true S: \"x\"")
61}