diff options
Diffstat (limited to 'libgo/go/database/sql/convert_test.go')
-rw-r--r-- | libgo/go/database/sql/convert_test.go | 114 |
1 files changed, 109 insertions, 5 deletions
diff --git a/libgo/go/database/sql/convert_test.go b/libgo/go/database/sql/convert_test.go index b0aff7b..8a82891 100644 --- a/libgo/go/database/sql/convert_test.go +++ b/libgo/go/database/sql/convert_test.go @@ -439,7 +439,7 @@ func TestDriverArgs(t *testing.T) { 0: { args: []interface{}{Valuer_V("foo")}, want: []driver.NamedValue{ - driver.NamedValue{ + { Ordinal: 1, Value: "FOO", }, @@ -448,7 +448,7 @@ func TestDriverArgs(t *testing.T) { 1: { args: []interface{}{nilValuerVPtr}, want: []driver.NamedValue{ - driver.NamedValue{ + { Ordinal: 1, Value: nil, }, @@ -457,7 +457,7 @@ func TestDriverArgs(t *testing.T) { 2: { args: []interface{}{nilValuerPPtr}, want: []driver.NamedValue{ - driver.NamedValue{ + { Ordinal: 1, Value: "nil-to-str", }, @@ -466,7 +466,7 @@ func TestDriverArgs(t *testing.T) { 3: { args: []interface{}{"plain-str"}, want: []driver.NamedValue{ - driver.NamedValue{ + { Ordinal: 1, Value: "plain-str", }, @@ -475,7 +475,7 @@ func TestDriverArgs(t *testing.T) { 4: { args: []interface{}{nilStrPtr}, want: []driver.NamedValue{ - driver.NamedValue{ + { Ordinal: 1, Value: nil, }, @@ -494,3 +494,107 @@ func TestDriverArgs(t *testing.T) { } } } + +type dec struct { + form byte + neg bool + coefficient [16]byte + exponent int32 +} + +func (d dec) Decompose(buf []byte) (form byte, negative bool, coefficient []byte, exponent int32) { + coef := make([]byte, 16) + copy(coef, d.coefficient[:]) + return d.form, d.neg, coef, d.exponent +} + +func (d *dec) Compose(form byte, negative bool, coefficient []byte, exponent int32) error { + switch form { + default: + return fmt.Errorf("unknown form %d", form) + case 1, 2: + d.form = form + d.neg = negative + return nil + case 0: + } + d.form = form + d.neg = negative + d.exponent = exponent + + // This isn't strictly correct, as the extra bytes could be all zero, + // ignore this for this test. + if len(coefficient) > 16 { + return fmt.Errorf("coefficent too large") + } + copy(d.coefficient[:], coefficient) + + return nil +} + +type decFinite struct { + neg bool + coefficient [16]byte + exponent int32 +} + +func (d decFinite) Decompose(buf []byte) (form byte, negative bool, coefficient []byte, exponent int32) { + coef := make([]byte, 16) + copy(coef, d.coefficient[:]) + return 0, d.neg, coef, d.exponent +} + +func (d *decFinite) Compose(form byte, negative bool, coefficient []byte, exponent int32) error { + switch form { + default: + return fmt.Errorf("unknown form %d", form) + case 1, 2: + return fmt.Errorf("unsupported form %d", form) + case 0: + } + d.neg = negative + d.exponent = exponent + + // This isn't strictly correct, as the extra bytes could be all zero, + // ignore this for this test. + if len(coefficient) > 16 { + return fmt.Errorf("coefficent too large") + } + copy(d.coefficient[:], coefficient) + + return nil +} + +func TestDecimal(t *testing.T) { + list := []struct { + name string + in decimalDecompose + out dec + err bool + }{ + {name: "same", in: dec{exponent: -6}, out: dec{exponent: -6}}, + + // Ensure reflection is not used to assign the value by using different types. + {name: "diff", in: decFinite{exponent: -6}, out: dec{exponent: -6}}, + + {name: "bad-form", in: dec{form: 200}, err: true}, + } + for _, item := range list { + t.Run(item.name, func(t *testing.T) { + out := dec{} + err := convertAssign(&out, item.in) + if item.err { + if err == nil { + t.Fatalf("unexpected nil error") + } + return + } + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if !reflect.DeepEqual(out, item.out) { + t.Fatalf("got %#v want %#v", out, item.out) + } + }) + } +} |