...

Source file src/victortoso.com/qapi-go/pkg/qapi/mixed_test.go

Documentation: victortoso.com/qapi-go/pkg/qapi

     1  package qapi
     2  
     3  import (
     4  	"encoding/json"
     5  	"math"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  type BuiltinTest struct {
    11  	SchemaStr    string
    12  	SchemaNumber float64
    13  	SchemaInt    int64
    14  	SchemaInt8   int8
    15  	SchemaInt16  int16
    16  	SchemaInt32  int32
    17  	SchemaInt64  int64
    18  	SchemaUint8  uint8
    19  	SchemaUint16 uint16
    20  	SchemaUint32 uint32
    21  	SchemaUint64 uint64
    22  	SchemaSize   uint64
    23  	SchemaBool   bool
    24  	SchemaNull   *string
    25  	SchemaAny    Any // Interesting results depend on provided JSON Object, See alternates.
    26  	SchemaQType  QType
    27  }
    28  
    29  func TestTypesBuiltin(t *testing.T) {
    30  	structToJson := []struct {
    31  		BuiltinTest
    32  		expectedOutput string
    33  	}{
    34  		// Upper limit check
    35  		{
    36  			BuiltinTest: BuiltinTest{
    37  				SchemaStr:    "a string",
    38  				SchemaNumber: math.MaxFloat64,
    39  				SchemaInt:    math.MaxInt64, // int -> int64 is defined in QEMU generator
    40  				SchemaInt8:   math.MaxInt8,
    41  				SchemaInt16:  math.MaxInt16,
    42  				SchemaInt32:  math.MaxInt32,
    43  				SchemaInt64:  math.MaxInt64,
    44  				SchemaUint8:  math.MaxUint8,
    45  				SchemaUint16: math.MaxUint16,
    46  				SchemaUint32: math.MaxUint32,
    47  				SchemaUint64: math.MaxUint64,
    48  				SchemaSize:   math.MaxUint64,
    49  				SchemaBool:   true,
    50  				SchemaNull:   nil,
    51  				SchemaAny:    nil,
    52  				SchemaQType:  QTypeNone,
    53  			},
    54  			expectedOutput: `{"SchemaStr":"a string","SchemaNumber":1.7976931348623157e+308,` +
    55  				`"SchemaInt":9223372036854775807,"SchemaInt8":127,"SchemaInt16":32767,` +
    56  				`"SchemaInt32":2147483647,"SchemaInt64":9223372036854775807,"SchemaUint8":255,` +
    57  				`"SchemaUint16":65535,"SchemaUint32":4294967295,"SchemaUint64":18446744073709551615,` +
    58  				`"SchemaSize":18446744073709551615,"SchemaBool":true,"SchemaNull":null,` +
    59  				`"SchemaAny":null,"SchemaQType":"none"}`,
    60  		},
    61  		// Lower limit check
    62  		{
    63  			BuiltinTest: BuiltinTest{
    64  				SchemaStr:    "another string",
    65  				SchemaNumber: math.SmallestNonzeroFloat64,
    66  				SchemaInt:    math.MinInt64, // int -> int64 is defined in QEMU generator
    67  				SchemaInt8:   math.MinInt8,
    68  				SchemaInt16:  math.MinInt16,
    69  				SchemaInt32:  math.MinInt32,
    70  				SchemaInt64:  math.MinInt64,
    71  				SchemaUint8:  0,
    72  				SchemaUint16: 0,
    73  				SchemaUint32: 0,
    74  				SchemaUint64: 0,
    75  				SchemaSize:   0,
    76  				SchemaBool:   false,
    77  				SchemaNull:   nil,
    78  				SchemaAny:    nil,
    79  				SchemaQType:  QTypeQbool,
    80  			},
    81  			expectedOutput: `{"SchemaStr":"another string","SchemaNumber":5e-324,` +
    82  				`"SchemaInt":-9223372036854775808,"SchemaInt8":-128,"SchemaInt16":-32768,` +
    83  				`"SchemaInt32":-2147483648,"SchemaInt64":-9223372036854775808,"SchemaUint8":0,` +
    84  				`"SchemaUint16":0,"SchemaUint32":0,"SchemaUint64":0,"SchemaSize":0,"SchemaBool":false,` +
    85  				`"SchemaNull":null,"SchemaAny":null,"SchemaQType":"qbool"}`,
    86  		},
    87  	}
    88  
    89  	for _, data := range structToJson {
    90  		if b, err := json.Marshal(&data); err != nil {
    91  			panic(err)
    92  		} else if strings.Compare(string(b), data.expectedOutput) != 0 {
    93  			t.Fatalf("(marshal) Bulting types do not match:\nGot:%s\nHas:%s\n", string(b), data.expectedOutput)
    94  		} else {
    95  			s := BuiltinTest{}
    96  			if err := json.Unmarshal(b, &s); err != nil {
    97  				panic(err)
    98  			} else if s != data.BuiltinTest {
    99  				t.Fatalf("(unmarshal) Bulting types do not match:\nGot:%v\nHas:%v\n", s, data.BuiltinTest)
   100  			}
   101  		}
   102  	}
   103  }
   104  
   105  func TestTypesStruct(t *testing.T) {
   106  	input := `{"file":"/some/place/my-image","backing":"/some/place/my-backing-file"}`
   107  	s := BlockdevOptionsGenericCOWFormat{}
   108  	if err := json.Unmarshal([]byte(input), &s); err != nil {
   109  		panic(err)
   110  	} else if strings.Compare(s.File.Value.(string), "/some/place/my-image") != 0 ||
   111  		strings.Compare(s.Backing.Value.(string), "/some/place/my-backing-file") != 0 {
   112  		t.Fatalf("(unmarshal) expected output failed:\nGot:%v\nHas:%s\n", s, input)
   113  	}
   114  
   115  	if b, err := json.Marshal(&s); err != nil {
   116  		panic(err)
   117  	} else if strings.Compare(string(b), input) != 0 {
   118  		t.Fatalf("(marshal) expected output failed:\nGot:%v\nHas:%s\n", string(b), input)
   119  	}
   120  }
   121  

View as plain text