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