github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/mat/format_test.go (about) 1 // Copyright ©2013 The Gonum 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 mat 6 7 import ( 8 "fmt" 9 "math" 10 "testing" 11 ) 12 13 func TestFormat(t *testing.T) { 14 t.Parallel() 15 type rp struct { 16 format string 17 output string 18 } 19 sqrt := func(_, _ int, v float64) float64 { return math.Sqrt(v) } 20 for i, test := range []struct { 21 m fmt.Formatter 22 rep []rp 23 }{ 24 // Dense matrix representation 25 { 26 m: Formatted(NewDense(3, 3, []float64{0, 0, 0, 0, 0, 0, 0, 0, 0})), 27 rep: []rp{ 28 {"%v", "⎡0 0 0⎤\n⎢0 0 0⎥\n⎣0 0 0⎦"}, 29 {"% f", "⎡. . .⎤\n⎢. . .⎥\n⎣. . .⎦"}, 30 {"%#v", fmt.Sprintf("%#v", NewDense(3, 3, nil))}, 31 {"%s", "%!s(*mat.Dense=Dims(3, 3))"}, 32 }, 33 }, 34 { 35 m: Formatted(NewDense(3, 3, []float64{1, 1, 1, 1, 1, 1, 1, 1, 1})), 36 rep: []rp{ 37 {"%v", "⎡1 1 1⎤\n⎢1 1 1⎥\n⎣1 1 1⎦"}, 38 {"% f", "⎡1 1 1⎤\n⎢1 1 1⎥\n⎣1 1 1⎦"}, 39 {"%#v", fmt.Sprintf("%#v", NewDense(3, 3, []float64{1, 1, 1, 1, 1, 1, 1, 1, 1}))}, 40 }, 41 }, 42 { 43 m: Formatted(NewDense(3, 3, []float64{1, 1, 1, 1, 1, 1, 1, 1, 1}), Prefix("\t")), 44 rep: []rp{ 45 {"%v", "⎡1 1 1⎤\n\t⎢1 1 1⎥\n\t⎣1 1 1⎦"}, 46 {"% f", "⎡1 1 1⎤\n\t⎢1 1 1⎥\n\t⎣1 1 1⎦"}, 47 {"%#v", fmt.Sprintf("%#v", NewDense(3, 3, []float64{1, 1, 1, 1, 1, 1, 1, 1, 1}))}, 48 }, 49 }, 50 { 51 m: Formatted(NewDense(3, 3, []float64{1, 0, 0, 0, 1, 0, 0, 0, 1})), 52 rep: []rp{ 53 {"%v", "⎡1 0 0⎤\n⎢0 1 0⎥\n⎣0 0 1⎦"}, 54 {"% f", "⎡1 . .⎤\n⎢. 1 .⎥\n⎣. . 1⎦"}, 55 {"%#v", fmt.Sprintf("%#v", NewDense(3, 3, []float64{1, 0, 0, 0, 1, 0, 0, 0, 1}))}, 56 }, 57 }, 58 { 59 m: Formatted(NewDense(2, 3, []float64{1, 2, 3, 4, 5, 6})), 60 rep: []rp{ 61 {"%v", "⎡1 2 3⎤\n⎣4 5 6⎦"}, 62 {"% f", "⎡1 2 3⎤\n⎣4 5 6⎦"}, 63 {"%#v", fmt.Sprintf("%#v", NewDense(2, 3, []float64{1, 2, 3, 4, 5, 6}))}, 64 }, 65 }, 66 { 67 m: Formatted(NewDense(3, 2, []float64{1, 2, 3, 4, 5, 6})), 68 rep: []rp{ 69 {"%v", "⎡1 2⎤\n⎢3 4⎥\n⎣5 6⎦"}, 70 {"% f", "⎡1 2⎤\n⎢3 4⎥\n⎣5 6⎦"}, 71 {"%#v", fmt.Sprintf("%#v", NewDense(3, 2, []float64{1, 2, 3, 4, 5, 6}))}, 72 }, 73 }, 74 { 75 m: func() fmt.Formatter { 76 m := NewDense(2, 3, []float64{0, 1, 2, 3, 4, 5}) 77 m.Apply(sqrt, m) 78 return Formatted(m) 79 }(), 80 rep: []rp{ 81 {"%v", "⎡ 0 1 1.4142135623730951⎤\n⎣1.7320508075688772 2 2.23606797749979⎦"}, 82 {"%.2f", "⎡0.00 1.00 1.41⎤\n⎣1.73 2.00 2.24⎦"}, 83 {"% f", "⎡ . 1 1.4142135623730951⎤\n⎣1.7320508075688772 2 2.23606797749979⎦"}, 84 {"%#v", fmt.Sprintf("%#v", NewDense(2, 3, []float64{0, 1, 1.4142135623730951, 1.7320508075688772, 2, 2.23606797749979}))}, 85 }, 86 }, 87 { 88 m: func() fmt.Formatter { 89 m := NewDense(3, 2, []float64{0, 1, 2, 3, 4, 5}) 90 m.Apply(sqrt, m) 91 return Formatted(m) 92 }(), 93 rep: []rp{ 94 {"%v", "⎡ 0 1⎤\n⎢1.4142135623730951 1.7320508075688772⎥\n⎣ 2 2.23606797749979⎦"}, 95 {"%.2f", "⎡0.00 1.00⎤\n⎢1.41 1.73⎥\n⎣2.00 2.24⎦"}, 96 {"% f", "⎡ . 1⎤\n⎢1.4142135623730951 1.7320508075688772⎥\n⎣ 2 2.23606797749979⎦"}, 97 {"%#v", fmt.Sprintf("%#v", NewDense(3, 2, []float64{0, 1, 1.4142135623730951, 1.7320508075688772, 2, 2.23606797749979}))}, 98 }, 99 }, 100 { 101 m: func() fmt.Formatter { 102 m := NewDense(2, 3, []float64{0, 1, 2, 3, 4, 5}) 103 m.Apply(sqrt, m) 104 return Formatted(m, Squeeze()) 105 }(), 106 rep: []rp{ 107 {"%v", "⎡ 0 1 1.4142135623730951⎤\n⎣1.7320508075688772 2 2.23606797749979⎦"}, 108 {"%.2f", "⎡0.00 1.00 1.41⎤\n⎣1.73 2.00 2.24⎦"}, 109 {"% f", "⎡ . 1 1.4142135623730951⎤\n⎣1.7320508075688772 2 2.23606797749979⎦"}, 110 {"%#v", fmt.Sprintf("%#v", NewDense(2, 3, []float64{0, 1, 1.4142135623730951, 1.7320508075688772, 2, 2.23606797749979}))}, 111 }, 112 }, 113 { 114 m: func() fmt.Formatter { 115 m := NewDense(1, 10, []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) 116 return Formatted(m, Excerpt(3)) 117 }(), 118 rep: []rp{ 119 {"%v", "Dims(1, 10)\n[ 1 2 3 ... ... 8 9 10]"}, 120 }, 121 }, 122 { 123 m: func() fmt.Formatter { 124 m := NewDense(10, 1, []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) 125 return Formatted(m, Excerpt(3)) 126 }(), 127 rep: []rp{ 128 {"%v", "Dims(10, 1)\n⎡ 1⎤\n⎢ 2⎥\n⎢ 3⎥\n .\n .\n .\n⎢ 8⎥\n⎢ 9⎥\n⎣10⎦"}, 129 }, 130 }, 131 { 132 m: func() fmt.Formatter { 133 m := NewDense(10, 10, nil) 134 for i := 0; i < 10; i++ { 135 m.Set(i, i, 1) 136 } 137 return Formatted(m, Excerpt(3)) 138 }(), 139 rep: []rp{ 140 {"%v", "Dims(10, 10)\n⎡1 0 0 ... ... 0 0 0⎤\n⎢0 1 0 0 0 0⎥\n⎢0 0 1 0 0 0⎥\n .\n .\n .\n⎢0 0 0 1 0 0⎥\n⎢0 0 0 0 1 0⎥\n⎣0 0 0 ... ... 0 0 1⎦"}, 141 }, 142 }, 143 { 144 m: Formatted(NewDense(9, 1, []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}), FormatMATLAB()), 145 rep: []rp{ 146 {"%v", "[1; 2; 3; 4; 5; 6; 7; 8; 9]"}, 147 {"%#v", "[\n 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n]"}, 148 {"%s", "%!s(*mat.Dense=Dims(9, 1))"}, 149 {"%#s", "%!s(*mat.Dense=Dims(9, 1))"}, 150 }, 151 }, 152 { 153 m: Formatted(NewDense(1, 9, []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}), FormatMATLAB()), 154 rep: []rp{ 155 {"%v", "[1 2 3 4 5 6 7 8 9]"}, 156 {"%#v", "[1 2 3 4 5 6 7 8 9]"}, 157 }, 158 }, 159 { 160 m: Formatted(NewDense(3, 3, []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}), FormatMATLAB()), 161 rep: []rp{ 162 {"%v", "[1 2 3; 4 5 6; 7 8 9]"}, 163 {"%#v", "[\n 1 2 3\n 4 5 6\n 7 8 9\n]"}, 164 }, 165 }, 166 { 167 m: Formatted(NewDense(9, 1, []float64{1, -2, 3, 4, 5, 6, 7, 8, 9}), FormatMATLAB()), 168 rep: []rp{ 169 {"%v", "[1; -2; 3; 4; 5; 6; 7; 8; 9]"}, 170 {"%#v", "[\n 1\n -2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n]"}, 171 }, 172 }, 173 { 174 m: Formatted(NewDense(1, 9, []float64{1, -2, 3, 4, 5, 6, 7, 8, 9}), FormatMATLAB()), 175 rep: []rp{ 176 {"%v", "[1 -2 3 4 5 6 7 8 9]"}, 177 {"%#v", "[ 1 -2 3 4 5 6 7 8 9]"}, 178 }, 179 }, 180 { 181 m: Formatted(NewDense(3, 3, []float64{1, -2, 3, 4, 5, 6, 7, 8, 9}), FormatMATLAB()), 182 rep: []rp{ 183 {"%v", "[1 -2 3; 4 5 6; 7 8 9]"}, 184 {"%#v", "[\n 1 -2 3\n 4 5 6\n 7 8 9\n]"}, 185 }, 186 }, 187 188 { 189 m: Formatted(NewDense(9, 1, []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}), FormatPython()), 190 rep: []rp{ 191 {"%v", "[[1], [2], [3], [4], [5], [6], [7], [8], [9]]"}, 192 {"%#v", "[[1],\n [2],\n [3],\n [4],\n [5],\n [6],\n [7],\n [8],\n [9]]"}, 193 {"%s", "%!s(*mat.Dense=Dims(9, 1))"}, 194 {"%#s", "%!s(*mat.Dense=Dims(9, 1))"}, 195 }, 196 }, 197 { 198 m: Formatted(NewDense(1, 9, []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}), FormatPython()), 199 rep: []rp{ 200 {"%v", "[1, 2, 3, 4, 5, 6, 7, 8, 9]"}, 201 {"%#v", "[1, 2, 3, 4, 5, 6, 7, 8, 9]"}, 202 }, 203 }, 204 { 205 m: Formatted(NewDense(3, 3, []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}), FormatPython()), 206 rep: []rp{ 207 {"%v", "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]"}, 208 {"%#v", "[[1, 2, 3],\n [4, 5, 6],\n [7, 8, 9]]"}, 209 }, 210 }, 211 { 212 m: Formatted(NewDense(9, 1, []float64{1, -2, 3, 4, 5, 6, 7, 8, 9}), FormatPython()), 213 rep: []rp{ 214 {"%v", "[[1], [-2], [3], [4], [5], [6], [7], [8], [9]]"}, 215 {"%#v", "[[ 1],\n [-2],\n [ 3],\n [ 4],\n [ 5],\n [ 6],\n [ 7],\n [ 8],\n [ 9]]"}, 216 }, 217 }, 218 { 219 m: Formatted(NewDense(1, 9, []float64{1, -2, 3, 4, 5, 6, 7, 8, 9}), FormatPython()), 220 rep: []rp{ 221 {"%v", "[1, -2, 3, 4, 5, 6, 7, 8, 9]"}, 222 {"%#v", "[ 1, -2, 3, 4, 5, 6, 7, 8, 9]"}, 223 }, 224 }, 225 { 226 m: Formatted(NewDense(3, 3, []float64{1, -2, 3, 4, 5, 6, 7, 8, 9}), FormatPython()), 227 rep: []rp{ 228 {"%v", "[[1, -2, 3], [4, 5, 6], [7, 8, 9]]"}, 229 {"%#v", "[[ 1, -2, 3],\n [ 4, 5, 6],\n [ 7, 8, 9]]"}, 230 }, 231 }, 232 } { 233 for j, rp := range test.rep { 234 got := fmt.Sprintf(rp.format, test.m) 235 if got != rp.output { 236 t.Errorf("unexpected format result test %d part %d:\ngot:\n%s\nwant:\n%s", i, j, got, rp.output) 237 } 238 } 239 } 240 }