2015-07-08 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / libgo / go / strconv / atob_test.go
blob28f469f5854d8a504674e88cab58a9f2d5b98d13
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 strconv_test
7 import (
8 "bytes"
9 . "strconv"
10 "testing"
13 type atobTest struct {
14 in string
15 out bool
16 err error
19 var atobtests = []atobTest{
20 {"", false, ErrSyntax},
21 {"asdf", false, ErrSyntax},
22 {"0", false, nil},
23 {"f", false, nil},
24 {"F", false, nil},
25 {"FALSE", false, nil},
26 {"false", false, nil},
27 {"False", false, nil},
28 {"1", true, nil},
29 {"t", true, nil},
30 {"T", true, nil},
31 {"TRUE", true, nil},
32 {"true", true, nil},
33 {"True", true, nil},
36 func TestParseBool(t *testing.T) {
37 for _, test := range atobtests {
38 b, e := ParseBool(test.in)
39 if test.err != nil {
40 // expect an error
41 if e == nil {
42 t.Errorf("%s: expected %s but got nil", test.in, test.err)
43 } else {
44 // NumError assertion must succeed; it's the only thing we return.
45 if test.err != e.(*NumError).Err {
46 t.Errorf("%s: expected %s but got %s", test.in, test.err, e)
49 } else {
50 if e != nil {
51 t.Errorf("%s: expected no error but got %s", test.in, e)
53 if b != test.out {
54 t.Errorf("%s: expected %t but got %t", test.in, test.out, b)
60 var boolString = map[bool]string{
61 true: "true",
62 false: "false",
65 func TestFormatBool(t *testing.T) {
66 for b, s := range boolString {
67 if f := FormatBool(b); f != s {
68 t.Errorf(`FormatBool(%v): expected %q but got %q`, b, s, f)
73 type appendBoolTest struct {
74 b bool
75 in []byte
76 out []byte
79 var appendBoolTests = []appendBoolTest{
80 {true, []byte("foo "), []byte("foo true")},
81 {false, []byte("foo "), []byte("foo false")},
84 func TestAppendBool(t *testing.T) {
85 for _, test := range appendBoolTests {
86 b := AppendBool(test.in, test.b)
87 if !bytes.Equal(b, test.out) {
88 t.Errorf("AppendBool(%q, %v): expected %q but got %q", test.in, test.b, test.out, b)