github.com/artyom/thrift@v0.0.0-20130902103359-388840a05deb/json_protocol_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 package thrift 21 22 import ( 23 "encoding/base64" 24 "encoding/json" 25 "fmt" 26 "math" 27 "strconv" 28 "testing" 29 ) 30 31 func TestWriteJSONProtocolBool(t *testing.T) { 32 thetype := "boolean" 33 trans := NewTMemoryBuffer() 34 p := NewTJSONProtocol(trans) 35 for _, value := range BOOL_VALUES { 36 if e := p.WriteBool(value); e != nil { 37 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) 38 } 39 if e := p.Flush(); e != nil { 40 t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) 41 } 42 s := trans.String() 43 if s != fmt.Sprint(value) { 44 t.Fatalf("Bad value for %s %v: %s", thetype, value, s) 45 } 46 v := false 47 if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { 48 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) 49 } 50 trans.Reset() 51 } 52 trans.Close() 53 } 54 55 func TestReadJSONProtocolBool(t *testing.T) { 56 thetype := "boolean" 57 for _, value := range BOOL_VALUES { 58 trans := NewTMemoryBuffer() 59 p := NewTJSONProtocol(trans) 60 if value { 61 trans.Write(JSON_TRUE) 62 } else { 63 trans.Write(JSON_FALSE) 64 } 65 trans.Flush() 66 s := trans.String() 67 v, e := p.ReadBool() 68 if e != nil { 69 t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) 70 } 71 if v != value { 72 t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) 73 } 74 if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { 75 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) 76 } 77 trans.Reset() 78 trans.Close() 79 } 80 } 81 82 func TestWriteJSONProtocolByte(t *testing.T) { 83 thetype := "byte" 84 trans := NewTMemoryBuffer() 85 p := NewTJSONProtocol(trans) 86 for _, value := range BYTE_VALUES { 87 if e := p.WriteByte(value); e != nil { 88 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) 89 } 90 if e := p.Flush(); e != nil { 91 t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) 92 } 93 s := trans.String() 94 if s != fmt.Sprint(value) { 95 t.Fatalf("Bad value for %s %v: %s", thetype, value, s) 96 } 97 v := byte(0) 98 if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { 99 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) 100 } 101 trans.Reset() 102 } 103 trans.Close() 104 } 105 106 func TestReadJSONProtocolByte(t *testing.T) { 107 thetype := "byte" 108 for _, value := range BYTE_VALUES { 109 trans := NewTMemoryBuffer() 110 p := NewTJSONProtocol(trans) 111 trans.WriteString(strconv.Itoa(int(value))) 112 trans.Flush() 113 s := trans.String() 114 v, e := p.ReadByte() 115 if e != nil { 116 t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) 117 } 118 if v != value { 119 t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) 120 } 121 if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { 122 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) 123 } 124 trans.Reset() 125 trans.Close() 126 } 127 } 128 129 func TestWriteJSONProtocolI16(t *testing.T) { 130 thetype := "int16" 131 trans := NewTMemoryBuffer() 132 p := NewTJSONProtocol(trans) 133 for _, value := range INT16_VALUES { 134 if e := p.WriteI16(value); e != nil { 135 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) 136 } 137 if e := p.Flush(); e != nil { 138 t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) 139 } 140 s := trans.String() 141 if s != fmt.Sprint(value) { 142 t.Fatalf("Bad value for %s %v: %s", thetype, value, s) 143 } 144 v := int16(0) 145 if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { 146 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) 147 } 148 trans.Reset() 149 } 150 trans.Close() 151 } 152 153 func TestReadJSONProtocolI16(t *testing.T) { 154 thetype := "int16" 155 for _, value := range INT16_VALUES { 156 trans := NewTMemoryBuffer() 157 p := NewTJSONProtocol(trans) 158 trans.WriteString(strconv.Itoa(int(value))) 159 trans.Flush() 160 s := trans.String() 161 v, e := p.ReadI16() 162 if e != nil { 163 t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) 164 } 165 if v != value { 166 t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) 167 } 168 if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { 169 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) 170 } 171 trans.Reset() 172 trans.Close() 173 } 174 } 175 176 func TestWriteJSONProtocolI32(t *testing.T) { 177 thetype := "int32" 178 trans := NewTMemoryBuffer() 179 p := NewTJSONProtocol(trans) 180 for _, value := range INT32_VALUES { 181 if e := p.WriteI32(value); e != nil { 182 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) 183 } 184 if e := p.Flush(); e != nil { 185 t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) 186 } 187 s := trans.String() 188 if s != fmt.Sprint(value) { 189 t.Fatalf("Bad value for %s %v: %s", thetype, value, s) 190 } 191 v := int32(0) 192 if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { 193 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) 194 } 195 trans.Reset() 196 } 197 trans.Close() 198 } 199 200 func TestReadJSONProtocolI32(t *testing.T) { 201 thetype := "int32" 202 for _, value := range INT32_VALUES { 203 trans := NewTMemoryBuffer() 204 p := NewTJSONProtocol(trans) 205 trans.WriteString(strconv.Itoa(int(value))) 206 trans.Flush() 207 s := trans.String() 208 v, e := p.ReadI32() 209 if e != nil { 210 t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) 211 } 212 if v != value { 213 t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) 214 } 215 if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { 216 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) 217 } 218 trans.Reset() 219 trans.Close() 220 } 221 } 222 223 func TestWriteJSONProtocolI64(t *testing.T) { 224 thetype := "int64" 225 trans := NewTMemoryBuffer() 226 p := NewTJSONProtocol(trans) 227 for _, value := range INT64_VALUES { 228 if e := p.WriteI64(value); e != nil { 229 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) 230 } 231 if e := p.Flush(); e != nil { 232 t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) 233 } 234 s := trans.String() 235 if s != fmt.Sprint(value) { 236 t.Fatalf("Bad value for %s %v: %s", thetype, value, s) 237 } 238 v := int64(0) 239 if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { 240 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) 241 } 242 trans.Reset() 243 } 244 trans.Close() 245 } 246 247 func TestReadJSONProtocolI64(t *testing.T) { 248 thetype := "int64" 249 for _, value := range INT64_VALUES { 250 trans := NewTMemoryBuffer() 251 p := NewTJSONProtocol(trans) 252 trans.WriteString(strconv.FormatInt(value, 10)) 253 trans.Flush() 254 s := trans.String() 255 v, e := p.ReadI64() 256 if e != nil { 257 t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) 258 } 259 if v != value { 260 t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) 261 } 262 if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { 263 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) 264 } 265 trans.Reset() 266 trans.Close() 267 } 268 } 269 270 func TestWriteJSONProtocolDouble(t *testing.T) { 271 thetype := "double" 272 trans := NewTMemoryBuffer() 273 p := NewTJSONProtocol(trans) 274 for _, value := range DOUBLE_VALUES { 275 if e := p.WriteDouble(value); e != nil { 276 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) 277 } 278 if e := p.Flush(); e != nil { 279 t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) 280 } 281 s := trans.String() 282 if math.IsInf(value, 1) { 283 if s != jsonQuote(JSON_INFINITY) { 284 t.Fatalf("Bad value for %s %v, wrote: %v, expected: %v", thetype, value, s, jsonQuote(JSON_INFINITY)) 285 } 286 } else if math.IsInf(value, -1) { 287 if s != jsonQuote(JSON_NEGATIVE_INFINITY) { 288 t.Fatalf("Bad value for %s %v, wrote: %v, expected: %v", thetype, value, s, jsonQuote(JSON_NEGATIVE_INFINITY)) 289 } 290 } else if math.IsNaN(value) { 291 if s != jsonQuote(JSON_NAN) { 292 t.Fatalf("Bad value for %s %v, wrote: %v, expected: %v", thetype, value, s, jsonQuote(JSON_NAN)) 293 } 294 } else { 295 if s != fmt.Sprint(value) { 296 t.Fatalf("Bad value for %s %v: %s", thetype, value, s) 297 } 298 v := float64(0) 299 if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { 300 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) 301 } 302 } 303 trans.Reset() 304 } 305 trans.Close() 306 } 307 308 func TestReadJSONProtocolDouble(t *testing.T) { 309 thetype := "double" 310 for _, value := range DOUBLE_VALUES { 311 trans := NewTMemoryBuffer() 312 p := NewTJSONProtocol(trans) 313 n := NewNumericFromDouble(value) 314 trans.WriteString(n.String()) 315 trans.Flush() 316 s := trans.String() 317 v, e := p.ReadDouble() 318 if e != nil { 319 t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) 320 } 321 if math.IsInf(value, 1) { 322 if !math.IsInf(v, 1) { 323 t.Fatalf("Bad value for %s %v, wrote: %v, received: %v", thetype, value, s, v) 324 } 325 } else if math.IsInf(value, -1) { 326 if !math.IsInf(v, -1) { 327 t.Fatalf("Bad value for %s %v, wrote: %v, received: %v", thetype, value, s, v) 328 } 329 } else if math.IsNaN(value) { 330 if !math.IsNaN(v) { 331 t.Fatalf("Bad value for %s %v, wrote: %v, received: %v", thetype, value, s, v) 332 } 333 } else { 334 if v != value { 335 t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) 336 } 337 if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { 338 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) 339 } 340 } 341 trans.Reset() 342 trans.Close() 343 } 344 } 345 346 func TestWriteJSONProtocolString(t *testing.T) { 347 thetype := "string" 348 trans := NewTMemoryBuffer() 349 p := NewTJSONProtocol(trans) 350 for _, value := range STRING_VALUES { 351 if e := p.WriteString(value); e != nil { 352 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) 353 } 354 if e := p.Flush(); e != nil { 355 t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) 356 } 357 s := trans.String() 358 if s[0] != '"' || s[len(s)-1] != '"' { 359 t.Fatalf("Bad value for %s '%v', wrote '%v', expected: %v", thetype, value, s, fmt.Sprint("\"", value, "\"")) 360 } 361 v := new(string) 362 if err := json.Unmarshal([]byte(s), v); err != nil || *v != value { 363 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, *v) 364 } 365 trans.Reset() 366 } 367 trans.Close() 368 } 369 370 func TestReadJSONProtocolString(t *testing.T) { 371 thetype := "string" 372 for _, value := range STRING_VALUES { 373 trans := NewTMemoryBuffer() 374 p := NewTJSONProtocol(trans) 375 trans.WriteString(jsonQuote(value)) 376 trans.Flush() 377 s := trans.String() 378 v, e := p.ReadString() 379 if e != nil { 380 t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) 381 } 382 if v != value { 383 t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) 384 } 385 v1 := new(string) 386 if err := json.Unmarshal([]byte(s), v1); err != nil || *v1 != value { 387 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, *v1) 388 } 389 trans.Reset() 390 trans.Close() 391 } 392 } 393 394 func TestWriteJSONProtocolBinary(t *testing.T) { 395 thetype := "binary" 396 value := protocol_bdata 397 b64value := make([]byte, base64.StdEncoding.EncodedLen(len(protocol_bdata))) 398 base64.StdEncoding.Encode(b64value, value) 399 b64String := string(b64value) 400 trans := NewTMemoryBuffer() 401 p := NewTJSONProtocol(trans) 402 if e := p.WriteBinary(value); e != nil { 403 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) 404 } 405 if e := p.Flush(); e != nil { 406 t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) 407 } 408 s := trans.String() 409 expectedString := fmt.Sprint("\"", b64String, "\"") 410 if s != expectedString { 411 t.Fatalf("Bad value for %s %v\n wrote: \"%v\"\nexpected: \"%v\"", thetype, value, s, expectedString) 412 } 413 v1, err := p.ReadBinary() 414 if err != nil { 415 t.Fatalf("Unable to read binary: %s", err.Error()) 416 } 417 if len(v1) != len(value) { 418 t.Fatalf("Invalid value for binary\nexpected: \"%v\"\n read: \"%v\"", value, v1) 419 } 420 for k, v := range value { 421 if v1[k] != v { 422 t.Fatalf("Invalid value for binary at %v\nexpected: \"%v\"\n read: \"%v\"", k, v, v1[k]) 423 } 424 } 425 trans.Close() 426 } 427 428 func TestReadJSONProtocolBinary(t *testing.T) { 429 thetype := "binary" 430 value := protocol_bdata 431 b64value := make([]byte, base64.StdEncoding.EncodedLen(len(protocol_bdata))) 432 base64.StdEncoding.Encode(b64value, value) 433 b64String := string(b64value) 434 trans := NewTMemoryBuffer() 435 p := NewTJSONProtocol(trans) 436 trans.WriteString(jsonQuote(b64String)) 437 trans.Flush() 438 s := trans.String() 439 v, e := p.ReadBinary() 440 if e != nil { 441 t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) 442 } 443 if len(v) != len(value) { 444 t.Fatalf("Bad value for %s value length %v, wrote: %v, received length: %v", thetype, len(value), s, len(v)) 445 } 446 for i := 0; i < len(v); i++ { 447 if v[i] != value[i] { 448 t.Fatalf("Bad value for %s at index %d value %v, wrote: %v, received: %v", thetype, i, value[i], s, v[i]) 449 } 450 } 451 v1 := new(string) 452 if err := json.Unmarshal([]byte(s), v1); err != nil || *v1 != b64String { 453 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, *v1) 454 } 455 trans.Reset() 456 trans.Close() 457 } 458 459 func TestWriteJSONProtocolList(t *testing.T) { 460 thetype := "list" 461 trans := NewTMemoryBuffer() 462 p := NewTJSONProtocol(trans) 463 p.WriteListBegin(TType(DOUBLE), len(DOUBLE_VALUES)) 464 for _, value := range DOUBLE_VALUES { 465 if e := p.WriteDouble(value); e != nil { 466 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) 467 } 468 } 469 p.WriteListEnd() 470 if e := p.Flush(); e != nil { 471 t.Fatalf("Unable to write %s due to error flushing: %s", thetype, e.Error()) 472 } 473 str := trans.String() 474 str1 := new([]interface{}) 475 err := json.Unmarshal([]byte(str), str1) 476 if err != nil { 477 t.Fatalf("Unable to decode %s, wrote: %s", thetype, str) 478 } 479 l := *str1 480 if len(l) < 2 { 481 t.Fatalf("List must be at least of length two to include metadata") 482 } 483 if l[0] != "dbl" { 484 t.Fatal("Invalid type for list, expected: ", STRING, ", but was: ", l[0]) 485 } 486 if int(l[1].(float64)) != len(DOUBLE_VALUES) { 487 t.Fatal("Invalid length for list, expected: ", len(DOUBLE_VALUES), ", but was: ", l[1]) 488 } 489 for k, value := range DOUBLE_VALUES { 490 s := l[k+2] 491 if math.IsInf(value, 1) { 492 if s.(string) != JSON_INFINITY { 493 t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_INFINITY), str) 494 } 495 } else if math.IsInf(value, 0) { 496 if s.(string) != JSON_NEGATIVE_INFINITY { 497 t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_NEGATIVE_INFINITY), str) 498 } 499 } else if math.IsNaN(value) { 500 if s.(string) != JSON_NAN { 501 t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_NAN), str) 502 } 503 } else { 504 if s.(float64) != value { 505 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s'", thetype, value, s) 506 } 507 } 508 trans.Reset() 509 } 510 trans.Close() 511 } 512 513 func TestWriteJSONProtocolSet(t *testing.T) { 514 thetype := "set" 515 trans := NewTMemoryBuffer() 516 p := NewTJSONProtocol(trans) 517 p.WriteSetBegin(TType(DOUBLE), len(DOUBLE_VALUES)) 518 for _, value := range DOUBLE_VALUES { 519 if e := p.WriteDouble(value); e != nil { 520 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) 521 } 522 } 523 p.WriteSetEnd() 524 if e := p.Flush(); e != nil { 525 t.Fatalf("Unable to write %s due to error flushing: %s", thetype, e.Error()) 526 } 527 str := trans.String() 528 str1 := new([]interface{}) 529 err := json.Unmarshal([]byte(str), str1) 530 if err != nil { 531 t.Fatalf("Unable to decode %s, wrote: %s", thetype, str) 532 } 533 l := *str1 534 if len(l) < 2 { 535 t.Fatalf("Set must be at least of length two to include metadata") 536 } 537 if l[0] != "dbl" { 538 t.Fatal("Invalid type for set, expected: ", DOUBLE, ", but was: ", l[0]) 539 } 540 if int(l[1].(float64)) != len(DOUBLE_VALUES) { 541 t.Fatal("Invalid length for set, expected: ", len(DOUBLE_VALUES), ", but was: ", l[1]) 542 } 543 for k, value := range DOUBLE_VALUES { 544 s := l[k+2] 545 if math.IsInf(value, 1) { 546 if s.(string) != JSON_INFINITY { 547 t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_INFINITY), str) 548 } 549 } else if math.IsInf(value, 0) { 550 if s.(string) != JSON_NEGATIVE_INFINITY { 551 t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_NEGATIVE_INFINITY), str) 552 } 553 } else if math.IsNaN(value) { 554 if s.(string) != JSON_NAN { 555 t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_NAN), str) 556 } 557 } else { 558 if s.(float64) != value { 559 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s'", thetype, value, s) 560 } 561 } 562 trans.Reset() 563 } 564 trans.Close() 565 } 566 567 func TestWriteJSONProtocolMap(t *testing.T) { 568 thetype := "map" 569 trans := NewTMemoryBuffer() 570 p := NewTJSONProtocol(trans) 571 p.WriteMapBegin(TType(I32), TType(DOUBLE), len(DOUBLE_VALUES)) 572 for k, value := range DOUBLE_VALUES { 573 if e := p.WriteI32(int32(k)); e != nil { 574 t.Fatalf("Unable to write %s key int32 value %v due to error: %s", thetype, k, e.Error()) 575 } 576 if e := p.WriteDouble(value); e != nil { 577 t.Fatalf("Unable to write %s value float64 value %v due to error: %s", thetype, value, e.Error()) 578 } 579 } 580 p.WriteMapEnd() 581 if e := p.Flush(); e != nil { 582 t.Fatalf("Unable to write %s due to error flushing: %s", thetype, e.Error()) 583 } 584 str := trans.String() 585 if str[0] != '[' || str[len(str)-1] != ']' { 586 t.Fatalf("Bad value for %s, wrote: %q, in go: %q", thetype, str, DOUBLE_VALUES) 587 } 588 expectedKeyType, expectedValueType, expectedSize, err := p.ReadMapBegin() 589 if err != nil { 590 t.Fatalf("Error while reading map begin: %s", err.Error()) 591 } 592 if expectedKeyType != I32 { 593 t.Fatal("Expected map key type ", I32, ", but was ", expectedKeyType) 594 } 595 if expectedValueType != DOUBLE { 596 t.Fatal("Expected map value type ", DOUBLE, ", but was ", expectedValueType) 597 } 598 if expectedSize != len(DOUBLE_VALUES) { 599 t.Fatal("Expected map size of ", len(DOUBLE_VALUES), ", but was ", expectedSize) 600 } 601 for k, value := range DOUBLE_VALUES { 602 ik, err := p.ReadI32() 603 if err != nil { 604 t.Fatalf("Bad key for %s index %v, wrote: %v, expected: %v, error: %s", thetype, k, ik, string(k), err.Error()) 605 } 606 if int(ik) != k { 607 t.Fatalf("Bad key for %s index %v, wrote: %v, expected: %v", thetype, k, ik, k) 608 } 609 dv, err := p.ReadDouble() 610 if err != nil { 611 t.Fatalf("Bad value for %s index %v, wrote: %v, expected: %v, error: %s", thetype, k, dv, value, err.Error()) 612 } 613 s := strconv.FormatFloat(dv, 'g', 10, 64) 614 if math.IsInf(value, 1) { 615 if !math.IsInf(dv, 1) { 616 t.Fatalf("Bad value for %s at index %v %v, wrote: %v, expected: %v", thetype, k, value, s, jsonQuote(JSON_INFINITY)) 617 } 618 } else if math.IsInf(value, 0) { 619 if !math.IsInf(dv, 0) { 620 t.Fatalf("Bad value for %s at index %v %v, wrote: %v, expected: %v", thetype, k, value, s, jsonQuote(JSON_NEGATIVE_INFINITY)) 621 } 622 } else if math.IsNaN(value) { 623 if !math.IsNaN(dv) { 624 t.Fatalf("Bad value for %s at index %v %v, wrote: %v, expected: %v", thetype, k, value, s, jsonQuote(JSON_NAN)) 625 } 626 } else { 627 expected := strconv.FormatFloat(value, 'g', 10, 64) 628 if s != expected { 629 t.Fatalf("Bad value for %s at index %v %v, wrote: %v, expected %v", thetype, k, value, s, expected) 630 } 631 v := float64(0) 632 if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { 633 t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) 634 } 635 } 636 trans.Reset() 637 } 638 trans.Close() 639 }