MapTest.cs
2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using NUnit.Framework;
using System;
using System.IO;
using System.Linq;
namespace UniJSON.MsgPack
{
[TestFixture]
public class MapTest
{
[Test]
public void fix_map()
{
var f = new MsgPackFormatter();
f.BeginMap(2);
f.Key("0"); f.Value(1);
f.Key("2"); f.Value(3);
f.EndMap();
var bytes =
f.GetStoreBytes();
;
Assert.AreEqual(new Byte[]{
0x82, // map2
0xa1, 0x30, // "0"
0x01, // 1
0xa1, 0x32, // "2"
0x03 // 3
}, bytes.ToEnumerable());
var value = MsgPackParser.Parse(bytes);
Assert.AreEqual(2, value.GetObjectCount());
Assert.AreEqual(1, value["0"].GetValue());
Assert.AreEqual(3, value["2"].GetValue());
}
[Test]
public void map16()
{
var w = new MsgPackFormatter();
int size = 18;
w.BeginMap(size);
for (int i = 0; i < size; ++i)
{
w.Value(i.ToString());
w.Value(i + 5);
}
var bytes = w.GetStoreBytes().ToEnumerable().ToArray();
var expected = new Byte[]{
0xde, // map18
0x0, 0x12, // 18
0xa1, 0x30, // "0"
0x5,
0xa1, 0x31, // "1"
0x6,
0xa1, 0x32, // "2"
0x7,
0xa1, 0x33, // "3"
0x8,
0xa1, 0x34, // "4"
0x9,
0xa1, 0x35, // "5"
0xa,
0xa1, 0x36, // "6"
0xb,
0xa1, 0x37, // "7"
0xc,
0xa1, 0x38, // "8"
0xd,
0xa1, 0x39, // "9"
0xe,
0xa2, 0x31, 0x30, // "10"
0xf,
0xa2, 0x31, 0x31, // "11"
0x10,
0xa2, 0x31, 0x32, // "12"
0x11,
0xa2, 0x31, 0x33, // "13"
0x12,
0xa2, 0x31, 0x34, // "14"
0x13,
0xa2, 0x31, 0x35, // "15"
0x14,
0xa2, 0x31, 0x36, // "16"
0x15,
0xa2, 0x31, 0x37, // "17",
0x16
};
Assert.AreEqual(expected, bytes);
var value = MsgPackParser.Parse(bytes);
Assert.AreEqual(15, value["10"].GetValue());
}
}
}