| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package iconvg |
| 6 |
|
| 7 |
import ( |
| 8 |
"image/color" |
| 9 |
"math" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
var naturalTestCases = []struct { |
| 14 |
in buffer |
| 15 |
want uint32 |
| 16 |
wantN int |
| 17 |
}{{ |
| 18 |
buffer{}, |
| 19 |
0, |
| 20 |
0, |
| 21 |
}, { |
| 22 |
buffer{0x28}, |
| 23 |
20, |
| 24 |
1, |
| 25 |
}, { |
| 26 |
buffer{0x59}, |
| 27 |
0, |
| 28 |
0, |
| 29 |
}, { |
| 30 |
buffer{0x59, 0x83}, |
| 31 |
8406, |
| 32 |
2, |
| 33 |
}, { |
| 34 |
buffer{0x07, 0x00, 0x80}, |
| 35 |
0, |
| 36 |
0, |
| 37 |
}, { |
| 38 |
buffer{0x07, 0x00, 0x80, 0x3f}, |
| 39 |
266338305, |
| 40 |
4, |
| 41 |
}} |
| 42 |
|
| 43 |
func TestDecodeNatural(t *testing.T) { |
| 44 |
for _, tc := range naturalTestCases { |
| 45 |
got, gotN := tc.in.decodeNatural() |
| 46 |
if got != tc.want || gotN != tc.wantN { |
| 47 |
t.Errorf("in=%x: got %v, %d, want %v, %d", tc.in, got, gotN, tc.want, tc.wantN) |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func TestEncodeNatural(t *testing.T) { |
| 53 |
for _, tc := range naturalTestCases { |
| 54 |
if tc.wantN == 0 { |
| 55 |
continue |
| 56 |
} |
| 57 |
var b buffer |
| 58 |
b.encodeNatural(tc.want) |
| 59 |
if got, want := string(b), string(tc.in); got != want { |
| 60 |
t.Errorf("value=%v:\ngot % x\nwant % x", tc.want, got, want) |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
var realTestCases = []struct { |
| 66 |
in buffer |
| 67 |
want float32 |
| 68 |
wantN int |
| 69 |
}{{ |
| 70 |
buffer{0x28}, |
| 71 |
20, |
| 72 |
1, |
| 73 |
}, { |
| 74 |
buffer{0x59, 0x83}, |
| 75 |
8406, |
| 76 |
2, |
| 77 |
}, { |
| 78 |
buffer{0x07, 0x00, 0x80, 0x3f}, |
| 79 |
1.000000476837158203125, |
| 80 |
4, |
| 81 |
}} |
| 82 |
|
| 83 |
func TestDecodeReal(t *testing.T) { |
| 84 |
for _, tc := range realTestCases { |
| 85 |
got, gotN := tc.in.decodeReal() |
| 86 |
if got != tc.want || gotN != tc.wantN { |
| 87 |
t.Errorf("in=%x: got %v, %d, want %v, %d", tc.in, got, gotN, tc.want, tc.wantN) |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func TestEncodeReal(t *testing.T) { |
| 93 |
for _, tc := range realTestCases { |
| 94 |
var b buffer |
| 95 |
b.encodeReal(tc.want) |
| 96 |
if got, want := string(b), string(tc.in); got != want { |
| 97 |
t.Errorf("value=%v:\ngot % x\nwant % x", tc.want, got, want) |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
var coordinateTestCases = []struct { |
| 103 |
in buffer |
| 104 |
want float32 |
| 105 |
wantN int |
| 106 |
}{{ |
| 107 |
buffer{0x8e}, |
| 108 |
7, |
| 109 |
1, |
| 110 |
}, { |
| 111 |
buffer{0x81, 0x87}, |
| 112 |
7.5, |
| 113 |
2, |
| 114 |
}, { |
| 115 |
buffer{0x03, 0x00, 0xf0, 0x40}, |
| 116 |
7.5, |
| 117 |
4, |
| 118 |
}, { |
| 119 |
buffer{0x07, 0x00, 0xf0, 0x40}, |
| 120 |
7.5000019073486328125, |
| 121 |
4, |
| 122 |
}} |
| 123 |
|
| 124 |
func TestDecodeCoordinate(t *testing.T) { |
| 125 |
for _, tc := range coordinateTestCases { |
| 126 |
got, gotN := tc.in.decodeCoordinate() |
| 127 |
if got != tc.want || gotN != tc.wantN { |
| 128 |
t.Errorf("in=%x: got %v, %d, want %v, %d", tc.in, got, gotN, tc.want, tc.wantN) |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
func TestEncodeCoordinate(t *testing.T) { |
| 134 |
for _, tc := range coordinateTestCases { |
| 135 |
if tc.want == 7.5 && tc.wantN == 4 { |
| 136 |
|
| 137 |
continue |
| 138 |
} |
| 139 |
var b buffer |
| 140 |
b.encodeCoordinate(tc.want) |
| 141 |
if got, want := string(b), string(tc.in); got != want { |
| 142 |
t.Errorf("value=%v:\ngot % x\nwant % x", tc.want, got, want) |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func trunc(x float32) float32 { |
| 148 |
u := math.Float32bits(x) |
| 149 |
u &^= 0x03 |
| 150 |
return math.Float32frombits(u) |
| 151 |
} |
| 152 |
|
| 153 |
var zeroToOneTestCases = []struct { |
| 154 |
in buffer |
| 155 |
want float32 |
| 156 |
wantN int |
| 157 |
}{{ |
| 158 |
buffer{0x0a}, |
| 159 |
1.0 / 24, |
| 160 |
1, |
| 161 |
}, { |
| 162 |
buffer{0x41, 0x1a}, |
| 163 |
1.0 / 9, |
| 164 |
2, |
| 165 |
}, { |
| 166 |
buffer{0x63, 0x0b, 0x36, 0x3b}, |
| 167 |
trunc(1.0 / 360), |
| 168 |
4, |
| 169 |
}} |
| 170 |
|
| 171 |
func TestDecodeZeroToOne(t *testing.T) { |
| 172 |
for _, tc := range zeroToOneTestCases { |
| 173 |
got, gotN := tc.in.decodeZeroToOne() |
| 174 |
if got != tc.want || gotN != tc.wantN { |
| 175 |
t.Errorf("in=%x: got %v, %d, want %v, %d", tc.in, got, gotN, tc.want, tc.wantN) |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
func TestEncodeZeroToOne(t *testing.T) { |
| 181 |
for _, tc := range zeroToOneTestCases { |
| 182 |
var b buffer |
| 183 |
b.encodeZeroToOne(tc.want) |
| 184 |
if got, want := string(b), string(tc.in); got != want { |
| 185 |
t.Errorf("value=%v:\ngot % x\nwant % x", tc.want, got, want) |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
var colorTestCases = []struct { |
| 191 |
in buffer |
| 192 |
decode func(buffer) (Color, int) |
| 193 |
encode func(*buffer, Color) |
| 194 |
want Color |
| 195 |
wantN int |
| 196 |
}{{ |
| 197 |
buffer{}, |
| 198 |
buffer.decodeColor1, |
| 199 |
(*buffer).encodeColor1, |
| 200 |
Color{}, |
| 201 |
0, |
| 202 |
}, { |
| 203 |
buffer{0x00}, |
| 204 |
buffer.decodeColor1, |
| 205 |
(*buffer).encodeColor1, |
| 206 |
RGBAColor(color.RGBA{0x00, 0x00, 0x00, 0xff}), |
| 207 |
1, |
| 208 |
}, { |
| 209 |
buffer{0x30}, |
| 210 |
buffer.decodeColor1, |
| 211 |
(*buffer).encodeColor1, |
| 212 |
RGBAColor(color.RGBA{0x40, 0xff, 0xc0, 0xff}), |
| 213 |
1, |
| 214 |
}, { |
| 215 |
buffer{0x7c}, |
| 216 |
buffer.decodeColor1, |
| 217 |
(*buffer).encodeColor1, |
| 218 |
RGBAColor(color.RGBA{0xff, 0xff, 0xff, 0xff}), |
| 219 |
1, |
| 220 |
}, { |
| 221 |
buffer{0x7d}, |
| 222 |
buffer.decodeColor1, |
| 223 |
(*buffer).encodeColor1, |
| 224 |
RGBAColor(color.RGBA{0xc0, 0xc0, 0xc0, 0xc0}), |
| 225 |
1, |
| 226 |
}, { |
| 227 |
buffer{0x7e}, |
| 228 |
buffer.decodeColor1, |
| 229 |
(*buffer).encodeColor1, |
| 230 |
RGBAColor(color.RGBA{0x80, 0x80, 0x80, 0x80}), |
| 231 |
1, |
| 232 |
}, { |
| 233 |
buffer{0x7f}, |
| 234 |
buffer.decodeColor1, |
| 235 |
(*buffer).encodeColor1, |
| 236 |
RGBAColor(color.RGBA{0x00, 0x00, 0x00, 0x00}), |
| 237 |
1, |
| 238 |
}, { |
| 239 |
buffer{0x80}, |
| 240 |
buffer.decodeColor1, |
| 241 |
(*buffer).encodeColor1, |
| 242 |
PaletteIndexColor(0x00), |
| 243 |
1, |
| 244 |
}, { |
| 245 |
buffer{0xbf}, |
| 246 |
buffer.decodeColor1, |
| 247 |
(*buffer).encodeColor1, |
| 248 |
PaletteIndexColor(0x3f), |
| 249 |
1, |
| 250 |
}, { |
| 251 |
buffer{0xc0}, |
| 252 |
buffer.decodeColor1, |
| 253 |
(*buffer).encodeColor1, |
| 254 |
CRegColor(0x00), |
| 255 |
1, |
| 256 |
}, { |
| 257 |
buffer{0xff}, |
| 258 |
buffer.decodeColor1, |
| 259 |
(*buffer).encodeColor1, |
| 260 |
CRegColor(0x3f), |
| 261 |
1, |
| 262 |
}, { |
| 263 |
buffer{0x01}, |
| 264 |
buffer.decodeColor2, |
| 265 |
(*buffer).encodeColor2, |
| 266 |
Color{}, |
| 267 |
0, |
| 268 |
}, { |
| 269 |
buffer{0x38, 0x0f}, |
| 270 |
buffer.decodeColor2, |
| 271 |
(*buffer).encodeColor2, |
| 272 |
RGBAColor(color.RGBA{0x33, 0x88, 0x00, 0xff}), |
| 273 |
2, |
| 274 |
}, { |
| 275 |
buffer{0x00, 0x02}, |
| 276 |
buffer.decodeColor3Direct, |
| 277 |
(*buffer).encodeColor3Direct, |
| 278 |
Color{}, |
| 279 |
0, |
| 280 |
}, { |
| 281 |
buffer{0x30, 0x66, 0x07}, |
| 282 |
buffer.decodeColor3Direct, |
| 283 |
(*buffer).encodeColor3Direct, |
| 284 |
RGBAColor(color.RGBA{0x30, 0x66, 0x07, 0xff}), |
| 285 |
3, |
| 286 |
}, { |
| 287 |
buffer{0x00, 0x00, 0x03}, |
| 288 |
buffer.decodeColor4, |
| 289 |
(*buffer).encodeColor4, |
| 290 |
Color{}, |
| 291 |
0, |
| 292 |
}, { |
| 293 |
buffer{0x30, 0x66, 0x07, 0x80}, |
| 294 |
buffer.decodeColor4, |
| 295 |
(*buffer).encodeColor4, |
| 296 |
RGBAColor(color.RGBA{0x30, 0x66, 0x07, 0x80}), |
| 297 |
4, |
| 298 |
}, { |
| 299 |
buffer{0x00, 0x04}, |
| 300 |
buffer.decodeColor3Indirect, |
| 301 |
(*buffer).encodeColor3Indirect, |
| 302 |
Color{}, |
| 303 |
0, |
| 304 |
}, { |
| 305 |
buffer{0x40, 0x7f, 0x82}, |
| 306 |
buffer.decodeColor3Indirect, |
| 307 |
(*buffer).encodeColor3Indirect, |
| 308 |
BlendColor(0x40, 0x7f, 0x82), |
| 309 |
3, |
| 310 |
}} |
| 311 |
|
| 312 |
func TestDecodeColor(t *testing.T) { |
| 313 |
for _, tc := range colorTestCases { |
| 314 |
got, gotN := tc.decode(tc.in) |
| 315 |
if got != tc.want || gotN != tc.wantN { |
| 316 |
t.Errorf("in=%x: got %v, %d, want %v, %d", tc.in, got, gotN, tc.want, tc.wantN) |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
func TestEncodeColor(t *testing.T) { |
| 322 |
for _, tc := range colorTestCases { |
| 323 |
if tc.wantN == 0 { |
| 324 |
continue |
| 325 |
} |
| 326 |
var b buffer |
| 327 |
tc.encode(&b, tc.want) |
| 328 |
if got, want := string(b), string(tc.in); got != want { |
| 329 |
t.Errorf("value=%v:\ngot % x\nwant % x", tc.want, got, want) |
| 330 |
} |
| 331 |
} |
| 332 |
} |