aboutsummaryrefslogtreecommitdiffhomepage
path: root/testdata
diff options
context:
space:
mode:
authorTakeshi Yoneda <[email protected]>2020-10-24 04:37:35 +0900
committerGitHub <[email protected]>2020-10-23 21:37:35 +0200
commit1dec9dcbc466583fb0aa16c337ddf6f47ee73cb4 (patch)
treeb33902e7611c03dc543a7be985f1552ac32e88f4 /testdata
parentff833ef998337caca46650cf87a806850f7b6e4e (diff)
downloadtinygo-1dec9dcbc466583fb0aa16c337ddf6f47ee73cb4.tar.gz
tinygo-1dec9dcbc466583fb0aa16c337ddf6f47ee73cb4.zip
implement reflect.Swapper
Signed-off-by: mathetake <[email protected]>
Diffstat (limited to 'testdata')
-rw-r--r--testdata/sort.go183
-rw-r--r--testdata/sort.txt122
2 files changed, 305 insertions, 0 deletions
diff --git a/testdata/sort.go b/testdata/sort.go
new file mode 100644
index 000000000..33647fd69
--- /dev/null
+++ b/testdata/sort.go
@@ -0,0 +1,183 @@
+package main
+
+import "sort"
+
+// sort.Slice implicitly uses reflect.Swapper
+
+func strings() {
+ data := []string{"aaaa", "cccc", "bbb", "fff", "ggg"}
+ sort.Slice(data, func(i, j int) bool {
+ return data[i] > data[j]
+ })
+ println("strings")
+ for _, d := range data {
+ println(d)
+ }
+}
+
+func int64s() {
+ sd := []int64{1, 6, 3, 2, 1923, 123, -123, -29, 3, 0, 1}
+ sort.Slice(sd, func(i, j int) bool {
+ return sd[i] > sd[j]
+ })
+ println("int64s")
+ for _, d := range sd {
+ println(d)
+ }
+
+ ud := []uint64{1, 6, 3, 2, 1923, 123, 29, 3, 0, 1}
+ sort.Slice(ud, func(i, j int) bool {
+ return ud[i] > ud[j]
+ })
+ println("uint64s")
+ for _, d := range ud {
+ println(d)
+ }
+}
+
+func int32s() {
+ sd := []int32{1, 6, 3, 2, 1923, 123, -123, -29, 3, 0, 1}
+ sort.Slice(sd, func(i, j int) bool {
+ return sd[i] > sd[j]
+ })
+ println("int32s")
+ for _, d := range sd {
+ println(d)
+ }
+
+ ud := []uint32{1, 6, 3, 2, 1923, 123, 29, 3, 0, 1}
+ sort.Slice(ud, func(i, j int) bool {
+ return ud[i] > ud[j]
+ })
+ println("uint32s")
+ for _, d := range ud {
+ println(d)
+ }
+}
+
+func int16s() {
+ sd := []int16{1, 6, 3, 2, 1923, 123, -123, -29, 3, 0, 1}
+ sort.Slice(sd, func(i, j int) bool {
+ return sd[i] > sd[j]
+ })
+ println("int16s")
+ for _, d := range sd {
+ println(d)
+ }
+
+ ud := []uint16{1, 6, 3, 2, 1923, 123, 29, 3, 0, 1}
+ sort.Slice(ud, func(i, j int) bool {
+ return ud[i] > ud[j]
+ })
+ println("uint16s")
+ for _, d := range ud {
+ println(d)
+ }
+}
+
+func int8s() {
+ sd := []int8{1, 6, 3, 2, 123, -123, -29, 3, 0, 1}
+ sort.Slice(sd, func(i, j int) bool {
+ return sd[i] > sd[j]
+ })
+ println("int8s")
+ for _, d := range sd {
+ println(d)
+ }
+
+ ud := []uint8{1, 6, 3, 2, 123, 29, 3, 0, 1}
+ sort.Slice(ud, func(i, j int) bool {
+ return ud[i] > ud[j]
+ })
+ println("uint8s")
+ for _, d := range ud {
+ println(d)
+ }
+}
+
+func ints() {
+ sd := []int{1, 6, 3, 2, 123, -123, -29, 3, 0, 1}
+ sort.Slice(sd, func(i, j int) bool {
+ return sd[i] > sd[j]
+ })
+ println("ints")
+ for _, d := range sd {
+ println(d)
+ }
+
+ ud := []uint{1, 6, 3, 2, 123, 29, 3, 0, 1}
+ sort.Slice(ud, func(i, j int) bool {
+ return ud[i] > ud[j]
+ })
+ println("uints")
+ for _, d := range ud {
+ println(d)
+ }
+}
+
+func structs() {
+ type s struct {
+ name string
+ a uint64
+ b uint32
+ c uint16
+ d int
+ e *struct {
+ aa uint16
+ bb int
+ }
+ }
+
+ data := []s{
+ {
+ name: "struct 1",
+ d: 100,
+ e: &struct {
+ aa uint16
+ bb int
+ }{aa: 123, bb: -10},
+ },
+ {
+ name: "struct 2",
+ d: 1,
+ e: &struct {
+ aa uint16
+ bb int
+ }{aa: 15, bb: 10},
+ },
+ {
+ name: "struct 3",
+ d: 10,
+ e: &struct {
+ aa uint16
+ bb int
+ }{aa: 31, bb: -1030},
+ },
+ {
+ name: "struct 4",
+ e: &struct {
+ aa uint16
+ bb int
+ }{},
+ },
+ }
+ sort.Slice(data, func(i, j int) bool {
+ di := data[i]
+ dj := data[j]
+ return di.d*di.e.bb > dj.d*dj.e.bb
+ })
+ println("structs")
+ for _, d := range data {
+ println(d.name)
+ }
+}
+
+func main() {
+ strings()
+ int64s()
+ int32s()
+ int16s()
+ int8s()
+ ints()
+ structs()
+}
diff --git a/testdata/sort.txt b/testdata/sort.txt
new file mode 100644
index 000000000..74d2ca0cf
--- /dev/null
+++ b/testdata/sort.txt
@@ -0,0 +1,122 @@
+strings
+ggg
+fff
+cccc
+bbb
+aaaa
+int64s
+1923
+123
+6
+3
+3
+2
+1
+1
+0
+-29
+-123
+uint64s
+1923
+123
+29
+6
+3
+3
+2
+1
+1
+0
+int32s
+1923
+123
+6
+3
+3
+2
+1
+1
+0
+-29
+-123
+uint32s
+1923
+123
+29
+6
+3
+3
+2
+1
+1
+0
+int16s
+1923
+123
+6
+3
+3
+2
+1
+1
+0
+-29
+-123
+uint16s
+1923
+123
+29
+6
+3
+3
+2
+1
+1
+0
+int8s
+123
+6
+3
+3
+2
+1
+1
+0
+-29
+-123
+uint8s
+123
+29
+6
+3
+3
+2
+1
+1
+0
+ints
+123
+6
+3
+3
+2
+1
+1
+0
+-29
+-123
+uints
+123
+29
+6
+3
+3
+2
+1
+1
+0
+structs
+struct 2
+struct 4
+struct 1
+struct 3