| 1 |
// Copyright (C) 2019 Yasuhiro Matsumoto <[email protected]>. |
| 2 |
// |
| 3 |
// Use of this source code is governed by an MIT-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
//go:build cgo |
| 7 |
// +build cgo |
| 8 |
|
| 9 |
package sqlite3 |
| 10 |
|
| 11 |
import ( |
| 12 |
"errors" |
| 13 |
"math" |
| 14 |
"reflect" |
| 15 |
"testing" |
| 16 |
) |
| 17 |
|
| 18 |
func TestCallbackArgCast(t *testing.T) { |
| 19 |
intConv := callbackSyntheticForTests(reflect.ValueOf(int64(math.MaxInt64)), nil) |
| 20 |
floatConv := callbackSyntheticForTests(reflect.ValueOf(float64(math.MaxFloat64)), nil) |
| 21 |
errConv := callbackSyntheticForTests(reflect.Value{}, errors.New("test")) |
| 22 |
|
| 23 |
tests := []struct { |
| 24 |
f callbackArgConverter |
| 25 |
o reflect.Value |
| 26 |
}{ |
| 27 |
{intConv, reflect.ValueOf(int8(-1))}, |
| 28 |
{intConv, reflect.ValueOf(int16(-1))}, |
| 29 |
{intConv, reflect.ValueOf(int32(-1))}, |
| 30 |
{intConv, reflect.ValueOf(uint8(math.MaxUint8))}, |
| 31 |
{intConv, reflect.ValueOf(uint16(math.MaxUint16))}, |
| 32 |
{intConv, reflect.ValueOf(uint32(math.MaxUint32))}, |
| 33 |
// Special case, int64->uint64 is only 1<<63 - 1, not 1<<64 - 1 |
| 34 |
{intConv, reflect.ValueOf(uint64(math.MaxInt64))}, |
| 35 |
{floatConv, reflect.ValueOf(float32(math.Inf(1)))}, |
| 36 |
} |
| 37 |
|
| 38 |
for _, test := range tests { |
| 39 |
conv := callbackArgCast{test.f, test.o.Type()} |
| 40 |
val, err := conv.Run(nil) |
| 41 |
if err != nil { |
| 42 |
t.Errorf("Couldn't convert to %s: %s", test.o.Type(), err) |
| 43 |
} else if !reflect.DeepEqual(val.Interface(), test.o.Interface()) { |
| 44 |
t.Errorf("Unexpected result from converting to %s: got %v, want %v", test.o.Type(), val.Interface(), test.o.Interface()) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
conv := callbackArgCast{errConv, reflect.TypeOf(int8(0))} |
| 49 |
_, err := conv.Run(nil) |
| 50 |
if err == nil { |
| 51 |
t.Errorf("Expected error during callbackArgCast, but got none") |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func TestCallbackConverters(t *testing.T) { |
| 56 |
tests := []struct { |
| 57 |
v any |
| 58 |
err bool |
| 59 |
}{ |
| 60 |
// Unfortunately, we can't tell which converter was returned, |
| 61 |
// but we can at least check which types can be converted. |
| 62 |
{[]byte{0}, false}, |
| 63 |
{"text", false}, |
| 64 |
{true, false}, |
| 65 |
{int8(0), false}, |
| 66 |
{int16(0), false}, |
| 67 |
{int32(0), false}, |
| 68 |
{int64(0), false}, |
| 69 |
{uint8(0), false}, |
| 70 |
{uint16(0), false}, |
| 71 |
{uint32(0), false}, |
| 72 |
{uint64(0), false}, |
| 73 |
{int(0), false}, |
| 74 |
{uint(0), false}, |
| 75 |
{float64(0), false}, |
| 76 |
{float32(0), false}, |
| 77 |
|
| 78 |
{func() {}, true}, |
| 79 |
{complex64(complex(0, 0)), true}, |
| 80 |
{complex128(complex(0, 0)), true}, |
| 81 |
{struct{}{}, true}, |
| 82 |
{map[string]string{}, true}, |
| 83 |
{[]string{}, true}, |
| 84 |
{(*int8)(nil), true}, |
| 85 |
{make(chan int), true}, |
| 86 |
} |
| 87 |
|
| 88 |
for _, test := range tests { |
| 89 |
_, err := callbackArg(reflect.TypeOf(test.v)) |
| 90 |
if test.err && err == nil { |
| 91 |
t.Errorf("Expected an error when converting %s, got no error", reflect.TypeOf(test.v)) |
| 92 |
} else if !test.err && err != nil { |
| 93 |
t.Errorf("Expected converter when converting %s, got error: %s", reflect.TypeOf(test.v), err) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
for _, test := range tests { |
| 98 |
_, err := callbackRet(reflect.TypeOf(test.v)) |
| 99 |
if test.err && err == nil { |
| 100 |
t.Errorf("Expected an error when converting %s, got no error", reflect.TypeOf(test.v)) |
| 101 |
} else if !test.err && err != nil { |
| 102 |
t.Errorf("Expected converter when converting %s, got error: %s", reflect.TypeOf(test.v), err) |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func TestCallbackReturnAny(t *testing.T) { |
| 108 |
udf := func() any { |
| 109 |
return 1 |
| 110 |
} |
| 111 |
|
| 112 |
typ := reflect.TypeOf(udf) |
| 113 |
_, err := callbackRet(typ.Out(0)) |
| 114 |
if err != nil { |
| 115 |
t.Errorf("Expected valid callback for any return type, got: %s", err) |
| 116 |
} |
| 117 |
} |