goki.dev/laser@v0.1.34/slices_test.go (about) 1 // Copyright (c) 2023, The Goki 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 5 package laser 6 7 import ( 8 "testing" 9 ) 10 11 type TstStruct struct { 12 Field1 string 13 } 14 15 func TestCopySlice(t *testing.T) { 16 var tof []float32 17 var tos []string 18 fmf := []float32{1, 2, 3} 19 fms := []string{"3", "4", "5"} 20 err := CopySliceRobust(&tof, fmf) 21 if err != nil { 22 t.Errorf("copy float: %s\n", err.Error()) 23 } 24 for i := range fmf { 25 if tof[i] != fmf[i] { 26 t.Errorf("tof %d: %g != %g\n", i, tof[i], fmf[i]) 27 } 28 } 29 err = CopySliceRobust(&tos, fms) 30 if err != nil { 31 t.Errorf("copy string: %s\n", err.Error()) 32 } 33 for i := range fms { 34 if tos[i] != fms[i] { 35 t.Errorf("tos %d: %s != %s\n", i, tos[i], fms[i]) 36 } 37 } 38 err = CopySliceRobust(&tof, fms) 39 if err != nil { 40 t.Errorf("copy float = string: %s\n", err.Error()) 41 } 42 for i := range fms { 43 if ToString(tof[i]) != fms[i] { 44 t.Errorf("tof %d: %g != %s\n", i, tof[i], fms[i]) 45 } 46 } 47 fms = fms[:2] 48 err = CopySliceRobust(&tof, fms) 49 if err != nil { 50 t.Errorf("copy float = string: %s\n", err.Error()) 51 } 52 if len(tof) != len(fms) { 53 t.Errorf("copy float = string: size not 2: %d\n", len(tof)) 54 } 55 for i := range fms { 56 if ToString(tof[i]) != fms[i] { 57 t.Errorf("tof %d: %g != %s\n", i, tof[i], fms[i]) 58 } 59 } 60 fms = append(fms, "7") 61 err = CopySliceRobust(&tof, fms) 62 if err != nil { 63 t.Errorf("copy float = string: %s\n", err.Error()) 64 } 65 if len(tof) != len(fms) { 66 t.Errorf("copy float = string: size not 3: %d\n", len(tof)) 67 } 68 for i := range fms { 69 if ToString(tof[i]) != fms[i] { 70 t.Errorf("tof %d: %g != %s\n", i, tof[i], fms[i]) 71 } 72 } 73 74 var toc [][]float32 75 fmc := [][]string{[]string{"1", "2"}, []string{"3", "4"}} 76 err = CopySliceRobust(&toc, fmc) 77 if err != nil { 78 t.Errorf("copy [][]float = [][]string: %s\n", err.Error()) 79 } 80 for i := range fmc { 81 fmci := fmc[i] 82 toci := toc[i] 83 for j := range fmci { 84 if ToString(toci[j]) != fmci[j] { 85 t.Errorf("toci,j %d,%d: %g != %s\n", i, j, toci[j], fmci[j]) 86 } 87 } 88 } 89 } 90 91 // test slice type functions 92 func TestSliceType(t *testing.T) { 93 var sl []string 94 95 ts := SliceElType(sl).String() 96 if ts != "string" { 97 t.Errorf("slice el type should be string, not: %v\n", ts) 98 } 99 100 ts = SliceElType(&sl).String() 101 if ts != "string" { 102 t.Errorf("slice el type should be string, not: %v\n", ts) 103 } 104 105 var slp []*string 106 107 ts = SliceElType(slp).String() 108 if ts != "*string" { 109 t.Errorf("slice el type should be *string, not: %v\n", ts) 110 } 111 112 ts = SliceElType(&slp).String() 113 if ts != "*string" { 114 t.Errorf("slice el type should be *string, not: %v\n", ts) 115 } 116 117 var slsl [][]string 118 119 ts = SliceElType(slsl).String() 120 if ts != "[]string" { 121 t.Errorf("slice el type should be []string, not: %v\n", ts) 122 } 123 124 ts = SliceElType(&slsl).String() 125 if ts != "[]string" { 126 t.Errorf("slice el type should be []string, not: %v\n", ts) 127 } 128 129 // fmt.Printf("slsl kind: %v\n", SliceElType(slsl).Kind()) 130 131 }