OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [encoding/] [json/] [decode_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2010 The Go 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 json
6
 
7
import (
8
        "bytes"
9
        "fmt"
10
        "reflect"
11
        "strings"
12
        "testing"
13
)
14
 
15
type T struct {
16
        X string
17
        Y int
18
        Z int `json:"-"`
19
}
20
 
21
type tx struct {
22
        x int
23
}
24
 
25
var txType = reflect.TypeOf((*tx)(nil)).Elem()
26
 
27
// A type that can unmarshal itself.
28
 
29
type unmarshaler struct {
30
        T bool
31
}
32
 
33
func (u *unmarshaler) UnmarshalJSON(b []byte) error {
34
        *u = unmarshaler{true} // All we need to see that UnmarshalJson is called.
35
        return nil
36
}
37
 
38
type ustruct struct {
39
        M unmarshaler
40
}
41
 
42
var (
43
        um0, um1 unmarshaler // target2 of unmarshaling
44
        ump      = &um1
45
        umtrue   = unmarshaler{true}
46
        umslice  = []unmarshaler{{true}}
47
        umslicep = new([]unmarshaler)
48
        umstruct = ustruct{unmarshaler{true}}
49
)
50
 
51
type unmarshalTest struct {
52
        in  string
53
        ptr interface{}
54
        out interface{}
55
        err error
56
}
57
 
58
var unmarshalTests = []unmarshalTest{
59
        // basic types
60
        {`true`, new(bool), true, nil},
61
        {`1`, new(int), 1, nil},
62
        {`1.2`, new(float64), 1.2, nil},
63
        {`-5`, new(int16), int16(-5), nil},
64
        {`"a\u1234"`, new(string), "a\u1234", nil},
65
        {`"http:\/\/"`, new(string), "http://", nil},
66
        {`"g-clef: \uD834\uDD1E"`, new(string), "g-clef: \U0001D11E", nil},
67
        {`"invalid: \uD834x\uDD1E"`, new(string), "invalid: \uFFFDx\uFFFD", nil},
68
        {"null", new(interface{}), nil, nil},
69
        {`{"X": [1,2,3], "Y": 4}`, new(T), T{Y: 4}, &UnmarshalTypeError{"array", reflect.TypeOf("")}},
70
        {`{"x": 1}`, new(tx), tx{}, &UnmarshalFieldError{"x", txType, txType.Field(0)}},
71
 
72
        // Z has a "-" tag.
73
        {`{"Y": 1, "Z": 2}`, new(T), T{Y: 1}, nil},
74
 
75
        // syntax errors
76
        {`{"X": "foo", "Y"}`, nil, nil, &SyntaxError{"invalid character '}' after object key", 17}},
77
        {`[1, 2, 3+]`, nil, nil, &SyntaxError{"invalid character '+' after array element", 9}},
78
 
79
        // array tests
80
        {`[1, 2, 3]`, new([3]int), [3]int{1, 2, 3}, nil},
81
        {`[1, 2, 3]`, new([1]int), [1]int{1}, nil},
82
        {`[1, 2, 3]`, new([5]int), [5]int{1, 2, 3, 0, 0}, nil},
83
 
84
        // composite tests
85
        {allValueIndent, new(All), allValue, nil},
86
        {allValueCompact, new(All), allValue, nil},
87
        {allValueIndent, new(*All), &allValue, nil},
88
        {allValueCompact, new(*All), &allValue, nil},
89
        {pallValueIndent, new(All), pallValue, nil},
90
        {pallValueCompact, new(All), pallValue, nil},
91
        {pallValueIndent, new(*All), &pallValue, nil},
92
        {pallValueCompact, new(*All), &pallValue, nil},
93
 
94
        // unmarshal interface test
95
        {`{"T":false}`, &um0, umtrue, nil}, // use "false" so test will fail if custom unmarshaler is not called
96
        {`{"T":false}`, &ump, &umtrue, nil},
97
        {`[{"T":false}]`, &umslice, umslice, nil},
98
        {`[{"T":false}]`, &umslicep, &umslice, nil},
99
        {`{"M":{"T":false}}`, &umstruct, umstruct, nil},
100
}
101
 
102
func TestMarshal(t *testing.T) {
103
        b, err := Marshal(allValue)
104
        if err != nil {
105
                t.Fatalf("Marshal allValue: %v", err)
106
        }
107
        if string(b) != allValueCompact {
108
                t.Errorf("Marshal allValueCompact")
109
                diff(t, b, []byte(allValueCompact))
110
                return
111
        }
112
 
113
        b, err = Marshal(pallValue)
114
        if err != nil {
115
                t.Fatalf("Marshal pallValue: %v", err)
116
        }
117
        if string(b) != pallValueCompact {
118
                t.Errorf("Marshal pallValueCompact")
119
                diff(t, b, []byte(pallValueCompact))
120
                return
121
        }
122
}
123
 
124
func TestMarshalBadUTF8(t *testing.T) {
125
        s := "hello\xffworld"
126
        b, err := Marshal(s)
127
        if err == nil {
128
                t.Fatal("Marshal bad UTF8: no error")
129
        }
130
        if len(b) != 0 {
131
                t.Fatal("Marshal returned data")
132
        }
133
        if _, ok := err.(*InvalidUTF8Error); !ok {
134
                t.Fatalf("Marshal did not return InvalidUTF8Error: %T %v", err, err)
135
        }
136
}
137
 
138
func TestUnmarshal(t *testing.T) {
139
        for i, tt := range unmarshalTests {
140
                var scan scanner
141
                in := []byte(tt.in)
142
                if err := checkValid(in, &scan); err != nil {
143
                        if !reflect.DeepEqual(err, tt.err) {
144
                                t.Errorf("#%d: checkValid: %#v", i, err)
145
                                continue
146
                        }
147
                }
148
                if tt.ptr == nil {
149
                        continue
150
                }
151
                // v = new(right-type)
152
                v := reflect.New(reflect.TypeOf(tt.ptr).Elem())
153
                if err := Unmarshal([]byte(in), v.Interface()); !reflect.DeepEqual(err, tt.err) {
154
                        t.Errorf("#%d: %v want %v", i, err, tt.err)
155
                        continue
156
                }
157
                if !reflect.DeepEqual(v.Elem().Interface(), tt.out) {
158
                        t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.Elem().Interface(), tt.out)
159
                        data, _ := Marshal(v.Elem().Interface())
160
                        println(string(data))
161
                        data, _ = Marshal(tt.out)
162
                        println(string(data))
163
                        continue
164
                }
165
        }
166
}
167
 
168
func TestUnmarshalMarshal(t *testing.T) {
169
        initBig()
170
        var v interface{}
171
        if err := Unmarshal(jsonBig, &v); err != nil {
172
                t.Fatalf("Unmarshal: %v", err)
173
        }
174
        b, err := Marshal(v)
175
        if err != nil {
176
                t.Fatalf("Marshal: %v", err)
177
        }
178
        if bytes.Compare(jsonBig, b) != 0 {
179
                t.Errorf("Marshal jsonBig")
180
                diff(t, b, jsonBig)
181
                return
182
        }
183
}
184
 
185
func TestLargeByteSlice(t *testing.T) {
186
        s0 := make([]byte, 2000)
187
        for i := range s0 {
188
                s0[i] = byte(i)
189
        }
190
        b, err := Marshal(s0)
191
        if err != nil {
192
                t.Fatalf("Marshal: %v", err)
193
        }
194
        var s1 []byte
195
        if err := Unmarshal(b, &s1); err != nil {
196
                t.Fatalf("Unmarshal: %v", err)
197
        }
198
        if bytes.Compare(s0, s1) != 0 {
199
                t.Errorf("Marshal large byte slice")
200
                diff(t, s0, s1)
201
        }
202
}
203
 
204
type Xint struct {
205
        X int
206
}
207
 
208
func TestUnmarshalInterface(t *testing.T) {
209
        var xint Xint
210
        var i interface{} = &xint
211
        if err := Unmarshal([]byte(`{"X":1}`), &i); err != nil {
212
                t.Fatalf("Unmarshal: %v", err)
213
        }
214
        if xint.X != 1 {
215
                t.Fatalf("Did not write to xint")
216
        }
217
}
218
 
219
func TestUnmarshalPtrPtr(t *testing.T) {
220
        var xint Xint
221
        pxint := &xint
222
        if err := Unmarshal([]byte(`{"X":1}`), &pxint); err != nil {
223
                t.Fatalf("Unmarshal: %v", err)
224
        }
225
        if xint.X != 1 {
226
                t.Fatalf("Did not write to xint")
227
        }
228
}
229
 
230
func TestEscape(t *testing.T) {
231
        const input = `"foobar"`
232
        const expected = `"\"foobar\"\u003chtml\u003e"`
233
        b, err := Marshal(input)
234
        if err != nil {
235
                t.Fatalf("Marshal error: %v", err)
236
        }
237
        if s := string(b); s != expected {
238
                t.Errorf("Encoding of [%s] was [%s], want [%s]", input, s, expected)
239
        }
240
}
241
 
242
func TestHTMLEscape(t *testing.T) {
243
        b, err := MarshalForHTML("foobarbaz<>&quux")
244
        if err != nil {
245
                t.Fatalf("MarshalForHTML error: %v", err)
246
        }
247
        if !bytes.Equal(b, []byte(`"foobarbaz\u003c\u003e\u0026quux"`)) {
248
                t.Fatalf("Unexpected encoding of \"<>&\": %s", b)
249
        }
250
}
251
 
252
// WrongString is a struct that's misusing the ,string modifier.
253
type WrongString struct {
254
        Message string `json:"result,string"`
255
}
256
 
257
type wrongStringTest struct {
258
        in, err string
259
}
260
 
261
var wrongStringTests = []wrongStringTest{
262
        {`{"result":"x"}`, `json: invalid use of ,string struct tag, trying to unmarshal "x" into string`},
263
        {`{"result":"foo"}`, `json: invalid use of ,string struct tag, trying to unmarshal "foo" into string`},
264
        {`{"result":"123"}`, `json: invalid use of ,string struct tag, trying to unmarshal "123" into string`},
265
}
266
 
267
// If people misuse the ,string modifier, the error message should be
268
// helpful, telling the user that they're doing it wrong.
269
func TestErrorMessageFromMisusedString(t *testing.T) {
270
        for n, tt := range wrongStringTests {
271
                r := strings.NewReader(tt.in)
272
                var s WrongString
273
                err := NewDecoder(r).Decode(&s)
274
                got := fmt.Sprintf("%v", err)
275
                if got != tt.err {
276
                        t.Errorf("%d. got err = %q, want %q", n, got, tt.err)
277
                }
278
        }
279
}
280
 
281
func noSpace(c rune) rune {
282
        if isSpace(c) {
283
                return -1
284
        }
285
        return c
286
}
287
 
288
type All struct {
289
        Bool    bool
290
        Int     int
291
        Int8    int8
292
        Int16   int16
293
        Int32   int32
294
        Int64   int64
295
        Uint    uint
296
        Uint8   uint8
297
        Uint16  uint16
298
        Uint32  uint32
299
        Uint64  uint64
300
        Uintptr uintptr
301
        Float32 float32
302
        Float64 float64
303
 
304
        Foo  string `json:"bar"`
305
        Foo2 string `json:"bar2,dummyopt"`
306
 
307
        IntStr int64 `json:",string"`
308
 
309
        PBool    *bool
310
        PInt     *int
311
        PInt8    *int8
312
        PInt16   *int16
313
        PInt32   *int32
314
        PInt64   *int64
315
        PUint    *uint
316
        PUint8   *uint8
317
        PUint16  *uint16
318
        PUint32  *uint32
319
        PUint64  *uint64
320
        PUintptr *uintptr
321
        PFloat32 *float32
322
        PFloat64 *float64
323
 
324
        String  string
325
        PString *string
326
 
327
        Map   map[string]Small
328
        MapP  map[string]*Small
329
        PMap  *map[string]Small
330
        PMapP *map[string]*Small
331
 
332
        EmptyMap map[string]Small
333
        NilMap   map[string]Small
334
 
335
        Slice   []Small
336
        SliceP  []*Small
337
        PSlice  *[]Small
338
        PSliceP *[]*Small
339
 
340
        EmptySlice []Small
341
        NilSlice   []Small
342
 
343
        StringSlice []string
344
        ByteSlice   []byte
345
 
346
        Small   Small
347
        PSmall  *Small
348
        PPSmall **Small
349
 
350
        Interface  interface{}
351
        PInterface *interface{}
352
 
353
        unexported int
354
}
355
 
356
type Small struct {
357
        Tag string
358
}
359
 
360
var allValue = All{
361
        Bool:    true,
362
        Int:     2,
363
        Int8:    3,
364
        Int16:   4,
365
        Int32:   5,
366
        Int64:   6,
367
        Uint:    7,
368
        Uint8:   8,
369
        Uint16:  9,
370
        Uint32:  10,
371
        Uint64:  11,
372
        Uintptr: 12,
373
        Float32: 14.1,
374
        Float64: 15.1,
375
        Foo:     "foo",
376
        Foo2:    "foo2",
377
        IntStr:  42,
378
        String:  "16",
379
        Map: map[string]Small{
380
                "17": {Tag: "tag17"},
381
                "18": {Tag: "tag18"},
382
        },
383
        MapP: map[string]*Small{
384
                "19": {Tag: "tag19"},
385
                "20": nil,
386
        },
387
        EmptyMap:    map[string]Small{},
388
        Slice:       []Small{{Tag: "tag20"}, {Tag: "tag21"}},
389
        SliceP:      []*Small{{Tag: "tag22"}, nil, {Tag: "tag23"}},
390
        EmptySlice:  []Small{},
391
        StringSlice: []string{"str24", "str25", "str26"},
392
        ByteSlice:   []byte{27, 28, 29},
393
        Small:       Small{Tag: "tag30"},
394
        PSmall:      &Small{Tag: "tag31"},
395
        Interface:   5.2,
396
}
397
 
398
var pallValue = All{
399
        PBool:      &allValue.Bool,
400
        PInt:       &allValue.Int,
401
        PInt8:      &allValue.Int8,
402
        PInt16:     &allValue.Int16,
403
        PInt32:     &allValue.Int32,
404
        PInt64:     &allValue.Int64,
405
        PUint:      &allValue.Uint,
406
        PUint8:     &allValue.Uint8,
407
        PUint16:    &allValue.Uint16,
408
        PUint32:    &allValue.Uint32,
409
        PUint64:    &allValue.Uint64,
410
        PUintptr:   &allValue.Uintptr,
411
        PFloat32:   &allValue.Float32,
412
        PFloat64:   &allValue.Float64,
413
        PString:    &allValue.String,
414
        PMap:       &allValue.Map,
415
        PMapP:      &allValue.MapP,
416
        PSlice:     &allValue.Slice,
417
        PSliceP:    &allValue.SliceP,
418
        PPSmall:    &allValue.PSmall,
419
        PInterface: &allValue.Interface,
420
}
421
 
422
var allValueIndent = `{
423
        "Bool": true,
424
        "Int": 2,
425
        "Int8": 3,
426
        "Int16": 4,
427
        "Int32": 5,
428
        "Int64": 6,
429
        "Uint": 7,
430
        "Uint8": 8,
431
        "Uint16": 9,
432
        "Uint32": 10,
433
        "Uint64": 11,
434
        "Uintptr": 12,
435
        "Float32": 14.1,
436
        "Float64": 15.1,
437
        "bar": "foo",
438
        "bar2": "foo2",
439
        "IntStr": "42",
440
        "PBool": null,
441
        "PInt": null,
442
        "PInt8": null,
443
        "PInt16": null,
444
        "PInt32": null,
445
        "PInt64": null,
446
        "PUint": null,
447
        "PUint8": null,
448
        "PUint16": null,
449
        "PUint32": null,
450
        "PUint64": null,
451
        "PUintptr": null,
452
        "PFloat32": null,
453
        "PFloat64": null,
454
        "String": "16",
455
        "PString": null,
456
        "Map": {
457
                "17": {
458
                        "Tag": "tag17"
459
                },
460
                "18": {
461
                        "Tag": "tag18"
462
                }
463
        },
464
        "MapP": {
465
                "19": {
466
                        "Tag": "tag19"
467
                },
468
                "20": null
469
        },
470
        "PMap": null,
471
        "PMapP": null,
472
        "EmptyMap": {},
473
        "NilMap": null,
474
        "Slice": [
475
                {
476
                        "Tag": "tag20"
477
                },
478
                {
479
                        "Tag": "tag21"
480
                }
481
        ],
482
        "SliceP": [
483
                {
484
                        "Tag": "tag22"
485
                },
486
                null,
487
                {
488
                        "Tag": "tag23"
489
                }
490
        ],
491
        "PSlice": null,
492
        "PSliceP": null,
493
        "EmptySlice": [],
494
        "NilSlice": null,
495
        "StringSlice": [
496
                "str24",
497
                "str25",
498
                "str26"
499
        ],
500
        "ByteSlice": "Gxwd",
501
        "Small": {
502
                "Tag": "tag30"
503
        },
504
        "PSmall": {
505
                "Tag": "tag31"
506
        },
507
        "PPSmall": null,
508
        "Interface": 5.2,
509
        "PInterface": null
510
}`
511
 
512
var allValueCompact = strings.Map(noSpace, allValueIndent)
513
 
514
var pallValueIndent = `{
515
        "Bool": false,
516
        "Int": 0,
517
        "Int8": 0,
518
        "Int16": 0,
519
        "Int32": 0,
520
        "Int64": 0,
521
        "Uint": 0,
522
        "Uint8": 0,
523
        "Uint16": 0,
524
        "Uint32": 0,
525
        "Uint64": 0,
526
        "Uintptr": 0,
527
        "Float32": 0,
528
        "Float64": 0,
529
        "bar": "",
530
        "bar2": "",
531
        "IntStr": "0",
532
        "PBool": true,
533
        "PInt": 2,
534
        "PInt8": 3,
535
        "PInt16": 4,
536
        "PInt32": 5,
537
        "PInt64": 6,
538
        "PUint": 7,
539
        "PUint8": 8,
540
        "PUint16": 9,
541
        "PUint32": 10,
542
        "PUint64": 11,
543
        "PUintptr": 12,
544
        "PFloat32": 14.1,
545
        "PFloat64": 15.1,
546
        "String": "",
547
        "PString": "16",
548
        "Map": null,
549
        "MapP": null,
550
        "PMap": {
551
                "17": {
552
                        "Tag": "tag17"
553
                },
554
                "18": {
555
                        "Tag": "tag18"
556
                }
557
        },
558
        "PMapP": {
559
                "19": {
560
                        "Tag": "tag19"
561
                },
562
                "20": null
563
        },
564
        "EmptyMap": null,
565
        "NilMap": null,
566
        "Slice": null,
567
        "SliceP": null,
568
        "PSlice": [
569
                {
570
                        "Tag": "tag20"
571
                },
572
                {
573
                        "Tag": "tag21"
574
                }
575
        ],
576
        "PSliceP": [
577
                {
578
                        "Tag": "tag22"
579
                },
580
                null,
581
                {
582
                        "Tag": "tag23"
583
                }
584
        ],
585
        "EmptySlice": null,
586
        "NilSlice": null,
587
        "StringSlice": null,
588
        "ByteSlice": null,
589
        "Small": {
590
                "Tag": ""
591
        },
592
        "PSmall": null,
593
        "PPSmall": {
594
                "Tag": "tag31"
595
        },
596
        "Interface": null,
597
        "PInterface": 5.2
598
}`
599
 
600
var pallValueCompact = strings.Map(noSpace, pallValueIndent)
601
 
602
func TestRefUnmarshal(t *testing.T) {
603
        type S struct {
604
                // Ref is defined in encode_test.go.
605
                R0 Ref
606
                R1 *Ref
607
        }
608
        want := S{
609
                R0: 12,
610
                R1: new(Ref),
611
        }
612
        *want.R1 = 12
613
 
614
        var got S
615
        if err := Unmarshal([]byte(`{"R0":"ref","R1":"ref"}`), &got); err != nil {
616
                t.Fatalf("Unmarshal: %v", err)
617
        }
618
        if !reflect.DeepEqual(got, want) {
619
                t.Errorf("got %+v, want %+v", got, want)
620
        }
621
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.