merkledag objects
Juan Batiz-Benet committed
Jul 4, 2014 at 16:55 UTC
dfbdf083e215ad8379b331c461da23a62a3f6eb2
5 files changed
+1366
-3
merkledag/coding.go
+12
@@ -59,3 +59,15 @@ func (n *Node) getPBNode() *PBNode {
59
pbn.Data = n.Data
60
return pbn
61
}
62
+
63
+func (n *Node) Encoded(force bool) ([]byte, error) {
64
+ if n.encoded == nil || force {
65
+ var err error
66
+ n.encoded, err = n.Marshal()
67
+ if err != nil {
68
+ return []byte{}, err
69
+ }
70
+ }
71
+
72
+ return n.encoded, nil
73
+}
merkledag/merkledag.go
+32
-3
@@ -9,6 +9,9 @@ import (
9
type Node struct {
10
Links []*Link
11
Data []byte
12
+
13
+ // cache encoded/marshaled value
14
+ encoded []byte
15
}
16
17
// An IPFS Merkle DAG Link
@@ -23,17 +26,43 @@ type Link struct {
26
Hash mh.Multihash
27
}
28
26
-type EncodedNode []byte
29
+func (n *Node) AddNodeLink(name string, that *Node) error {
30
+ s, err := that.Size()
31
+ if err != nil {
32
+ return err
33
+ }
34
+
35
+ h, err := that.Multihash()
36
+ if err != nil {
37
+ return err
38
+ }
39
+
40
+ n.Links = append(n.Links, &Link{
41
+ Name: name,
42
+ Size: s,
43
+ Hash: h,
44
+ })
45
+ return nil
46
+}
47
48
func (n *Node) Size() (uint64, error) {
29
- d, err := n.Marshal()
49
+ b, err := n.Encoded(false)
50
if err != nil {
51
return 0, err
52
}
53
34
- s := uint64(len(d))
54
+ s := uint64(len(b))
55
for _, l := range n.Links {
56
s += l.Size
57
}
58
return s, nil
59
}
60
+
61
+func (n *Node) Multihash() (mh.Multihash, error) {
62
+ b, err := n.Encoded(false)
63
+ if err != nil {
64
+ return nil, err
65
+ }
66
+
67
+ return mh.Sum(b, mh.SHA2_256, -1)
68
+}
merkledag/merkledag_test.go
new
+48
@@ -0,0 +1,48 @@
1
+package merkledag
2
+
3
+import (
4
+ "fmt"
5
+ // mh "github.com/jbenet/go-multihash"
6
+ "testing"
7
+)
8
+
9
+func TestNode(t *testing.T) {
10
+
11
+ n1 := &Node{Data: []byte("beep")}
12
+ n2 := &Node{Data: []byte("boop")}
13
+ n3 := &Node{Data: []byte("beep boop")}
14
+ if err := n3.AddNodeLink("beep-link", n1); err != nil {
15
+ t.Error(err)
16
+ }
17
+ if err := n3.AddNodeLink("boop-link", n2); err != nil {
18
+ t.Error(err)
19
+ }
20
+
21
+ printn := func(name string, n *Node) {
22
+ fmt.Println(">", name)
23
+ fmt.Println("data:", string(n.Data))
24
+
25
+ fmt.Println("links:")
26
+ for _, l := range n.Links {
27
+ fmt.Println("-", l.Name, l.Size, l.Hash)
28
+ }
29
+
30
+ e, err := n.Encoded(false)
31
+ if err != nil {
32
+ t.Error(err)
33
+ } else {
34
+ fmt.Println("encoded:", e)
35
+ }
36
+
37
+ h, err := n.Multihash()
38
+ if err != nil {
39
+ t.Error(err)
40
+ } else {
41
+ fmt.Println("hash:", h)
42
+ }
43
+ }
44
+
45
+ printn("beep", n1)
46
+ printn("boop", n2)
47
+ printn("beep boop", n3)
48
+}
merkledag/node.pb.go
new
+802
@@ -0,0 +1,802 @@
1
+// Code generated by protoc-gen-gogo.
2
+// source: node.proto
3
+// DO NOT EDIT!
4
+
5
+/*
6
+ Package merkledag is a generated protocol buffer package.
7
+
8
+ It is generated from these files:
9
+ node.proto
10
+
11
+ It has these top-level messages:
12
+ PBLink
13
+ PBNode
14
+*/
15
+package merkledag
16
+
17
+import proto "code.google.com/p/gogoprotobuf/proto"
18
+import json "encoding/json"
19
+import math "math"
20
+
21
+// discarding unused import gogoproto "code.google.com/p/gogoprotobuf/gogoproto/gogo.pb"
22
+
23
+import io "io"
24
+import code_google_com_p_gogoprotobuf_proto "code.google.com/p/gogoprotobuf/proto"
25
+
26
+import fmt "fmt"
27
+import strings "strings"
28
+import reflect "reflect"
29
+
30
+import fmt1 "fmt"
31
+import strings1 "strings"
32
+import code_google_com_p_gogoprotobuf_proto1 "code.google.com/p/gogoprotobuf/proto"
33
+import sort "sort"
34
+import strconv "strconv"
35
+import reflect1 "reflect"
36
+
37
+import fmt2 "fmt"
38
+import bytes "bytes"
39
+
40
+// Reference proto, json, and math imports to suppress error if they are not otherwise used.
41
+var _ = proto.Marshal
42
+var _ = &json.SyntaxError{}
43
+var _ = math.Inf
44
+
45
+// An IPFS MerkleDAG Link
46
+type PBLink struct {
47
+ // multihash of the target object
48
+ Hash []byte `protobuf:"bytes,1,opt" json:"Hash,omitempty"`
49
+ // utf string name. should be unique per object
50
+ Name *string `protobuf:"bytes,2,opt" json:"Name,omitempty"`
51
+ // cumulative size of target object
52
+ Tsize *uint64 `protobuf:"varint,3,opt" json:"Tsize,omitempty"`
53
+ XXX_unrecognized []byte `json:"-"`
54
+}
55
+
56
+func (m *PBLink) Reset() { *m = PBLink{} }
57
+func (*PBLink) ProtoMessage() {}
58
+
59
+func (m *PBLink) GetHash() []byte {
60
+ if m != nil {
61
+ return m.Hash
62
+ }
63
+ return nil
64
+}
65
+
66
+func (m *PBLink) GetName() string {
67
+ if m != nil && m.Name != nil {
68
+ return *m.Name
69
+ }
70
+ return ""
71
+}
72
+
73
+func (m *PBLink) GetTsize() uint64 {
74
+ if m != nil && m.Tsize != nil {
75
+ return *m.Tsize
76
+ }
77
+ return 0
78
+}
79
+
80
+// An IPFS MerkleDAG Node
81
+type PBNode struct {
82
+ // refs to other objects
83
+ Links []*PBLink `protobuf:"bytes,2,rep" json:"Links,omitempty"`
84
+ // opaque user data
85
+ Data []byte `protobuf:"bytes,1,opt" json:"Data,omitempty"`
86
+ XXX_unrecognized []byte `json:"-"`
87
+}
88
+
89
+func (m *PBNode) Reset() { *m = PBNode{} }
90
+func (*PBNode) ProtoMessage() {}
91
+
92
+func (m *PBNode) GetLinks() []*PBLink {
93
+ if m != nil {
94
+ return m.Links
95
+ }
96
+ return nil
97
+}
98
+
99
+func (m *PBNode) GetData() []byte {
100
+ if m != nil {
101
+ return m.Data
102
+ }
103
+ return nil
104
+}
105
+
106
+func init() {
107
+}
108
+func (m *PBLink) Unmarshal(data []byte) error {
109
+ l := len(data)
110
+ index := 0
111
+ for index < l {
112
+ var wire uint64
113
+ for shift := uint(0); ; shift += 7 {
114
+ if index >= l {
115
+ return io.ErrUnexpectedEOF
116
+ }
117
+ b := data[index]
118
+ index++
119
+ wire |= (uint64(b) & 0x7F) << shift
120
+ if b < 0x80 {
121
+ break
122
+ }
123
+ }
124
+ fieldNum := int32(wire >> 3)
125
+ wireType := int(wire & 0x7)
126
+ switch fieldNum {
127
+ case 1:
128
+ if wireType != 2 {
129
+ return code_google_com_p_gogoprotobuf_proto.ErrWrongType
130
+ }
131
+ var byteLen int
132
+ for shift := uint(0); ; shift += 7 {
133
+ if index >= l {
134
+ return io.ErrUnexpectedEOF
135
+ }
136
+ b := data[index]
137
+ index++
138
+ byteLen |= (int(b) & 0x7F) << shift
139
+ if b < 0x80 {
140
+ break
141
+ }
142
+ }
143
+ postIndex := index + byteLen
144
+ if postIndex > l {
145
+ return io.ErrUnexpectedEOF
146
+ }
147
+ m.Hash = append(m.Hash, data[index:postIndex]...)
148
+ index = postIndex
149
+ case 2:
150
+ if wireType != 2 {
151
+ return code_google_com_p_gogoprotobuf_proto.ErrWrongType
152
+ }
153
+ var stringLen uint64
154
+ for shift := uint(0); ; shift += 7 {
155
+ if index >= l {
156
+ return io.ErrUnexpectedEOF
157
+ }
158
+ b := data[index]
159
+ index++
160
+ stringLen |= (uint64(b) & 0x7F) << shift
161
+ if b < 0x80 {
162
+ break
163
+ }
164
+ }
165
+ postIndex := index + int(stringLen)
166
+ if postIndex > l {
167
+ return io.ErrUnexpectedEOF
168
+ }
169
+ s := string(data[index:postIndex])
170
+ m.Name = &s
171
+ index = postIndex
172
+ case 3:
173
+ if wireType != 0 {
174
+ return code_google_com_p_gogoprotobuf_proto.ErrWrongType
175
+ }
176
+ var v uint64
177
+ for shift := uint(0); ; shift += 7 {
178
+ if index >= l {
179
+ return io.ErrUnexpectedEOF
180
+ }
181
+ b := data[index]
182
+ index++
183
+ v |= (uint64(b) & 0x7F) << shift
184
+ if b < 0x80 {
185
+ break
186
+ }
187
+ }
188
+ m.Tsize = &v
189
+ default:
190
+ var sizeOfWire int
191
+ for {
192
+ sizeOfWire++
193
+ wire >>= 7
194
+ if wire == 0 {
195
+ break
196
+ }
197
+ }
198
+ index -= sizeOfWire
199
+ skippy, err := code_google_com_p_gogoprotobuf_proto.Skip(data[index:])
200
+ if err != nil {
201
+ return err
202
+ }
203
+ if (index + skippy) > l {
204
+ return io.ErrUnexpectedEOF
205
+ }
206
+ m.XXX_unrecognized = append(m.XXX_unrecognized, data[index:index+skippy]...)
207
+ index += skippy
208
+ }
209
+ }
210
+ return nil
211
+}
212
+func (m *PBNode) Unmarshal(data []byte) error {
213
+ l := len(data)
214
+ index := 0
215
+ for index < l {
216
+ var wire uint64
217
+ for shift := uint(0); ; shift += 7 {
218
+ if index >= l {
219
+ return io.ErrUnexpectedEOF
220
+ }
221
+ b := data[index]
222
+ index++
223
+ wire |= (uint64(b) & 0x7F) << shift
224
+ if b < 0x80 {
225
+ break
226
+ }
227
+ }
228
+ fieldNum := int32(wire >> 3)
229
+ wireType := int(wire & 0x7)
230
+ switch fieldNum {
231
+ case 2:
232
+ if wireType != 2 {
233
+ return code_google_com_p_gogoprotobuf_proto.ErrWrongType
234
+ }
235
+ var msglen int
236
+ for shift := uint(0); ; shift += 7 {
237
+ if index >= l {
238
+ return io.ErrUnexpectedEOF
239
+ }
240
+ b := data[index]
241
+ index++
242
+ msglen |= (int(b) & 0x7F) << shift
243
+ if b < 0x80 {
244
+ break
245
+ }
246
+ }
247
+ postIndex := index + msglen
248
+ if postIndex > l {
249
+ return io.ErrUnexpectedEOF
250
+ }
251
+ m.Links = append(m.Links, &PBLink{})
252
+ m.Links[len(m.Links)-1].Unmarshal(data[index:postIndex])
253
+ index = postIndex
254
+ case 1:
255
+ if wireType != 2 {
256
+ return code_google_com_p_gogoprotobuf_proto.ErrWrongType
257
+ }
258
+ var byteLen int
259
+ for shift := uint(0); ; shift += 7 {
260
+ if index >= l {
261
+ return io.ErrUnexpectedEOF
262
+ }
263
+ b := data[index]
264
+ index++
265
+ byteLen |= (int(b) & 0x7F) << shift
266
+ if b < 0x80 {
267
+ break
268
+ }
269
+ }
270
+ postIndex := index + byteLen
271
+ if postIndex > l {
272
+ return io.ErrUnexpectedEOF
273
+ }
274
+ m.Data = append(m.Data, data[index:postIndex]...)
275
+ index = postIndex
276
+ default:
277
+ var sizeOfWire int
278
+ for {
279
+ sizeOfWire++
280
+ wire >>= 7
281
+ if wire == 0 {
282
+ break
283
+ }
284
+ }
285
+ index -= sizeOfWire
286
+ skippy, err := code_google_com_p_gogoprotobuf_proto.Skip(data[index:])
287
+ if err != nil {
288
+ return err
289
+ }
290
+ if (index + skippy) > l {
291
+ return io.ErrUnexpectedEOF
292
+ }
293
+ m.XXX_unrecognized = append(m.XXX_unrecognized, data[index:index+skippy]...)
294
+ index += skippy
295
+ }
296
+ }
297
+ return nil
298
+}
299
+func (this *PBLink) String() string {
300
+ if this == nil {
301
+ return "nil"
302
+ }
303
+ s := strings.Join([]string{`&PBLink{`,
304
+ `Hash:` + valueToStringNode(this.Hash) + `,`,
305
+ `Name:` + valueToStringNode(this.Name) + `,`,
306
+ `Tsize:` + valueToStringNode(this.Tsize) + `,`,
307
+ `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`,
308
+ `}`,
309
+ }, "")
310
+ return s
311
+}
312
+func (this *PBNode) String() string {
313
+ if this == nil {
314
+ return "nil"
315
+ }
316
+ s := strings.Join([]string{`&PBNode{`,
317
+ `Links:` + strings.Replace(fmt.Sprintf("%v", this.Links), "PBLink", "PBLink", 1) + `,`,
318
+ `Data:` + valueToStringNode(this.Data) + `,`,
319
+ `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`,
320
+ `}`,
321
+ }, "")
322
+ return s
323
+}
324
+func valueToStringNode(v interface{}) string {
325
+ rv := reflect.ValueOf(v)
326
+ if rv.IsNil() {
327
+ return "nil"
328
+ }
329
+ pv := reflect.Indirect(rv).Interface()
330
+ return fmt.Sprintf("*%v", pv)
331
+}
332
+func (m *PBLink) Size() (n int) {
333
+ var l int
334
+ _ = l
335
+ if m.Hash != nil {
336
+ l = len(m.Hash)
337
+ n += 1 + l + sovNode(uint64(l))
338
+ }
339
+ if m.Name != nil {
340
+ l = len(*m.Name)
341
+ n += 1 + l + sovNode(uint64(l))
342
+ }
343
+ if m.Tsize != nil {
344
+ n += 1 + sovNode(uint64(*m.Tsize))
345
+ }
346
+ if m.XXX_unrecognized != nil {
347
+ n += len(m.XXX_unrecognized)
348
+ }
349
+ return n
350
+}
351
+func (m *PBNode) Size() (n int) {
352
+ var l int
353
+ _ = l
354
+ if len(m.Links) > 0 {
355
+ for _, e := range m.Links {
356
+ l = e.Size()
357
+ n += 1 + l + sovNode(uint64(l))
358
+ }
359
+ }
360
+ if m.Data != nil {
361
+ l = len(m.Data)
362
+ n += 1 + l + sovNode(uint64(l))
363
+ }
364
+ if m.XXX_unrecognized != nil {
365
+ n += len(m.XXX_unrecognized)
366
+ }
367
+ return n
368
+}
369
+
370
+func sovNode(x uint64) (n int) {
371
+ for {
372
+ n++
373
+ x >>= 7
374
+ if x == 0 {
375
+ break
376
+ }
377
+ }
378
+ return n
379
+}
380
+func sozNode(x uint64) (n int) {
381
+ return sovNode(uint64((x << 1) ^ uint64((int64(x) >> 63))))
382
+}
383
+func NewPopulatedPBLink(r randyNode, easy bool) *PBLink {
384
+ this := &PBLink{}
385
+ if r.Intn(10) != 0 {
386
+ v1 := r.Intn(100)
387
+ this.Hash = make([]byte, v1)
388
+ for i := 0; i < v1; i++ {
389
+ this.Hash[i] = byte(r.Intn(256))
390
+ }
391
+ }
392
+ if r.Intn(10) != 0 {
393
+ v2 := randStringNode(r)
394
+ this.Name = &v2
395
+ }
396
+ if r.Intn(10) != 0 {
397
+ v3 := uint64(r.Uint32())
398
+ this.Tsize = &v3
399
+ }
400
+ if !easy && r.Intn(10) != 0 {
401
+ this.XXX_unrecognized = randUnrecognizedNode(r, 4)
402
+ }
403
+ return this
404
+}
405
+
406
+func NewPopulatedPBNode(r randyNode, easy bool) *PBNode {
407
+ this := &PBNode{}
408
+ if r.Intn(10) != 0 {
409
+ v4 := r.Intn(10)
410
+ this.Links = make([]*PBLink, v4)
411
+ for i := 0; i < v4; i++ {
412
+ this.Links[i] = NewPopulatedPBLink(r, easy)
413
+ }
414
+ }
415
+ if r.Intn(10) != 0 {
416
+ v5 := r.Intn(100)
417
+ this.Data = make([]byte, v5)
418
+ for i := 0; i < v5; i++ {
419
+ this.Data[i] = byte(r.Intn(256))
420
+ }
421
+ }
422
+ if !easy && r.Intn(10) != 0 {
423
+ this.XXX_unrecognized = randUnrecognizedNode(r, 3)
424
+ }
425
+ return this
426
+}
427
+
428
+type randyNode interface {
429
+ Float32() float32
430
+ Float64() float64
431
+ Int63() int64
432
+ Int31() int32
433
+ Uint32() uint32
434
+ Intn(n int) int
435
+}
436
+
437
+func randUTF8RuneNode(r randyNode) rune {
438
+ res := rune(r.Uint32() % 1112064)
439
+ if 55296 <= res {
440
+ res += 2047
441
+ }
442
+ return res
443
+}
444
+func randStringNode(r randyNode) string {
445
+ v6 := r.Intn(100)
446
+ tmps := make([]rune, v6)
447
+ for i := 0; i < v6; i++ {
448
+ tmps[i] = randUTF8RuneNode(r)
449
+ }
450
+ return string(tmps)
451
+}
452
+func randUnrecognizedNode(r randyNode, maxFieldNumber int) (data []byte) {
453
+ l := r.Intn(5)
454
+ for i := 0; i < l; i++ {
455
+ wire := r.Intn(4)
456
+ if wire == 3 {
457
+ wire = 5
458
+ }
459
+ fieldNumber := maxFieldNumber + r.Intn(100)
460
+ data = randFieldNode(data, r, fieldNumber, wire)
461
+ }
462
+ return data
463
+}
464
+func randFieldNode(data []byte, r randyNode, fieldNumber int, wire int) []byte {
465
+ key := uint32(fieldNumber)<<3 | uint32(wire)
466
+ switch wire {
467
+ case 0:
468
+ data = encodeVarintPopulateNode(data, uint64(key))
469
+ v7 := r.Int63()
470
+ if r.Intn(2) == 0 {
471
+ v7 *= -1
472
+ }
473
+ data = encodeVarintPopulateNode(data, uint64(v7))
474
+ case 1:
475
+ data = encodeVarintPopulateNode(data, uint64(key))
476
+ data = append(data, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)))
477
+ case 2:
478
+ data = encodeVarintPopulateNode(data, uint64(key))
479
+ ll := r.Intn(100)
480
+ data = encodeVarintPopulateNode(data, uint64(ll))
481
+ for j := 0; j < ll; j++ {
482
+ data = append(data, byte(r.Intn(256)))
483
+ }
484
+ default:
485
+ data = encodeVarintPopulateNode(data, uint64(key))
486
+ data = append(data, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)))
487
+ }
488
+ return data
489
+}
490
+func encodeVarintPopulateNode(data []byte, v uint64) []byte {
491
+ for v >= 1<<7 {
492
+ data = append(data, uint8(uint64(v)&0x7f|0x80))
493
+ v >>= 7
494
+ }
495
+ data = append(data, uint8(v))
496
+ return data
497
+}
498
+func (m *PBLink) Marshal() (data []byte, err error) {
499
+ size := m.Size()
500
+ data = make([]byte, size)
501
+ n, err := m.MarshalTo(data)
502
+ if err != nil {
503
+ return nil, err
504
+ }
505
+ return data[:n], nil
506
+}
507
+
508
+func (m *PBLink) MarshalTo(data []byte) (n int, err error) {
509
+ var i int
510
+ _ = i
511
+ var l int
512
+ _ = l
513
+ if m.Hash != nil {
514
+ data[i] = 0xa
515
+ i++
516
+ i = encodeVarintNode(data, i, uint64(len(m.Hash)))
517
+ i += copy(data[i:], m.Hash)
518
+ }
519
+ if m.Name != nil {
520
+ data[i] = 0x12
521
+ i++
522
+ i = encodeVarintNode(data, i, uint64(len(*m.Name)))
523
+ i += copy(data[i:], *m.Name)
524
+ }
525
+ if m.Tsize != nil {
526
+ data[i] = 0x18
527
+ i++
528
+ i = encodeVarintNode(data, i, uint64(*m.Tsize))
529
+ }
530
+ if m.XXX_unrecognized != nil {
531
+ i += copy(data[i:], m.XXX_unrecognized)
532
+ }
533
+ return i, nil
534
+}
535
+func (m *PBNode) Marshal() (data []byte, err error) {
536
+ size := m.Size()
537
+ data = make([]byte, size)
538
+ n, err := m.MarshalTo(data)
539
+ if err != nil {
540
+ return nil, err
541
+ }
542
+ return data[:n], nil
543
+}
544
+
545
+func (m *PBNode) MarshalTo(data []byte) (n int, err error) {
546
+ var i int
547
+ _ = i
548
+ var l int
549
+ _ = l
550
+ if len(m.Links) > 0 {
551
+ for _, msg := range m.Links {
552
+ data[i] = 0x12
553
+ i++
554
+ i = encodeVarintNode(data, i, uint64(msg.Size()))
555
+ n, err := msg.MarshalTo(data[i:])
556
+ if err != nil {
557
+ return 0, err
558
+ }
559
+ i += n
560
+ }
561
+ }
562
+ if m.Data != nil {
563
+ data[i] = 0xa
564
+ i++
565
+ i = encodeVarintNode(data, i, uint64(len(m.Data)))
566
+ i += copy(data[i:], m.Data)
567
+ }
568
+ if m.XXX_unrecognized != nil {
569
+ i += copy(data[i:], m.XXX_unrecognized)
570
+ }
571
+ return i, nil
572
+}
573
+func encodeFixed64Node(data []byte, offset int, v uint64) int {
574
+ data[offset] = uint8(v)
575
+ data[offset+1] = uint8(v >> 8)
576
+ data[offset+2] = uint8(v >> 16)
577
+ data[offset+3] = uint8(v >> 24)
578
+ data[offset+4] = uint8(v >> 32)
579
+ data[offset+5] = uint8(v >> 40)
580
+ data[offset+6] = uint8(v >> 48)
581
+ data[offset+7] = uint8(v >> 56)
582
+ return offset + 8
583
+}
584
+func encodeFixed32Node(data []byte, offset int, v uint32) int {
585
+ data[offset] = uint8(v)
586
+ data[offset+1] = uint8(v >> 8)
587
+ data[offset+2] = uint8(v >> 16)
588
+ data[offset+3] = uint8(v >> 24)
589
+ return offset + 4
590
+}
591
+func encodeVarintNode(data []byte, offset int, v uint64) int {
592
+ for v >= 1<<7 {
593
+ data[offset] = uint8(v&0x7f | 0x80)
594
+ v >>= 7
595
+ offset++
596
+ }
597
+ data[offset] = uint8(v)
598
+ return offset + 1
599
+}
600
+func (this *PBLink) GoString() string {
601
+ if this == nil {
602
+ return "nil"
603
+ }
604
+ s := strings1.Join([]string{`&merkledag.PBLink{` + `Hash:` + valueToGoStringNode(this.Hash, "byte"), `Name:` + valueToGoStringNode(this.Name, "string"), `Tsize:` + valueToGoStringNode(this.Tsize, "uint64"), `XXX_unrecognized:` + fmt1.Sprintf("%#v", this.XXX_unrecognized) + `}`}, ", ")
605
+ return s
606
+}
607
+func (this *PBNode) GoString() string {
608
+ if this == nil {
609
+ return "nil"
610
+ }
611
+ s := strings1.Join([]string{`&merkledag.PBNode{` + `Links:` + fmt1.Sprintf("%#v", this.Links), `Data:` + valueToGoStringNode(this.Data, "byte"), `XXX_unrecognized:` + fmt1.Sprintf("%#v", this.XXX_unrecognized) + `}`}, ", ")
612
+ return s
613
+}
614
+func valueToGoStringNode(v interface{}, typ string) string {
615
+ rv := reflect1.ValueOf(v)
616
+ if rv.IsNil() {
617
+ return "nil"
618
+ }
619
+ pv := reflect1.Indirect(rv).Interface()
620
+ return fmt1.Sprintf("func(v %v) *%v { return &v } ( %#v )", typ, typ, pv)
621
+}
622
+func extensionToGoStringNode(e map[int32]code_google_com_p_gogoprotobuf_proto1.Extension) string {
623
+ if e == nil {
624
+ return "nil"
625
+ }
626
+ s := "map[int32]proto.Extension{"
627
+ keys := make([]int, 0, len(e))
628
+ for k := range e {
629
+ keys = append(keys, int(k))
630
+ }
631
+ sort.Ints(keys)
632
+ ss := []string{}
633
+ for _, k := range keys {
634
+ ss = append(ss, strconv.Itoa(k)+": "+e[int32(k)].GoString())
635
+ }
636
+ s += strings1.Join(ss, ",") + "}"
637
+ return s
638
+}
639
+func (this *PBLink) VerboseEqual(that interface{}) error {
640
+ if that == nil {
641
+ if this == nil {
642
+ return nil
643
+ }
644
+ return fmt2.Errorf("that == nil && this != nil")
645
+ }
646
+
647
+ that1, ok := that.(*PBLink)
648
+ if !ok {
649
+ return fmt2.Errorf("that is not of type *PBLink")
650
+ }
651
+ if that1 == nil {
652
+ if this == nil {
653
+ return nil
654
+ }
655
+ return fmt2.Errorf("that is type *PBLink but is nil && this != nil")
656
+ } else if this == nil {
657
+ return fmt2.Errorf("that is type *PBLinkbut is not nil && this == nil")
658
+ }
659
+ if !bytes.Equal(this.Hash, that1.Hash) {
660
+ return fmt2.Errorf("Hash this(%v) Not Equal that(%v)", this.Hash, that1.Hash)
661
+ }
662
+ if this.Name != nil && that1.Name != nil {
663
+ if *this.Name != *that1.Name {
664
+ return fmt2.Errorf("Name this(%v) Not Equal that(%v)", *this.Name, *that1.Name)
665
+ }
666
+ } else if this.Name != nil {
667
+ return fmt2.Errorf("this.Name == nil && that.Name != nil")
668
+ } else if that1.Name != nil {
669
+ return fmt2.Errorf("Name this(%v) Not Equal that(%v)", this.Name, that1.Name)
670
+ }
671
+ if this.Tsize != nil && that1.Tsize != nil {
672
+ if *this.Tsize != *that1.Tsize {
673
+ return fmt2.Errorf("Tsize this(%v) Not Equal that(%v)", *this.Tsize, *that1.Tsize)
674
+ }
675
+ } else if this.Tsize != nil {
676
+ return fmt2.Errorf("this.Tsize == nil && that.Tsize != nil")
677
+ } else if that1.Tsize != nil {
678
+ return fmt2.Errorf("Tsize this(%v) Not Equal that(%v)", this.Tsize, that1.Tsize)
679
+ }
680
+ if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) {
681
+ return fmt2.Errorf("XXX_unrecognized this(%v) Not Equal that(%v)", this.XXX_unrecognized, that1.XXX_unrecognized)
682
+ }
683
+ return nil
684
+}
685
+func (this *PBLink) Equal(that interface{}) bool {
686
+ if that == nil {
687
+ if this == nil {
688
+ return true
689
+ }
690
+ return false
691
+ }
692
+
693
+ that1, ok := that.(*PBLink)
694
+ if !ok {
695
+ return false
696
+ }
697
+ if that1 == nil {
698
+ if this == nil {
699
+ return true
700
+ }
701
+ return false
702
+ } else if this == nil {
703
+ return false
704
+ }
705
+ if !bytes.Equal(this.Hash, that1.Hash) {
706
+ return false
707
+ }
708
+ if this.Name != nil && that1.Name != nil {
709
+ if *this.Name != *that1.Name {
710
+ return false
711
+ }
712
+ } else if this.Name != nil {
713
+ return false
714
+ } else if that1.Name != nil {
715
+ return false
716
+ }
717
+ if this.Tsize != nil && that1.Tsize != nil {
718
+ if *this.Tsize != *that1.Tsize {
719
+ return false
720
+ }
721
+ } else if this.Tsize != nil {
722
+ return false
723
+ } else if that1.Tsize != nil {
724
+ return false
725
+ }
726
+ if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) {
727
+ return false
728
+ }
729
+ return true
730
+}
731
+func (this *PBNode) VerboseEqual(that interface{}) error {
732
+ if that == nil {
733
+ if this == nil {
734
+ return nil
735
+ }
736
+ return fmt2.Errorf("that == nil && this != nil")
737
+ }
738
+
739
+ that1, ok := that.(*PBNode)
740
+ if !ok {
741
+ return fmt2.Errorf("that is not of type *PBNode")
742
+ }
743
+ if that1 == nil {
744
+ if this == nil {
745
+ return nil
746
+ }
747
+ return fmt2.Errorf("that is type *PBNode but is nil && this != nil")
748
+ } else if this == nil {
749
+ return fmt2.Errorf("that is type *PBNodebut is not nil && this == nil")
750
+ }
751
+ if len(this.Links) != len(that1.Links) {
752
+ return fmt2.Errorf("Links this(%v) Not Equal that(%v)", len(this.Links), len(that1.Links))
753
+ }
754
+ for i := range this.Links {
755
+ if !this.Links[i].Equal(that1.Links[i]) {
756
+ return fmt2.Errorf("Links this[%v](%v) Not Equal that[%v](%v)", i, this.Links[i], i, that1.Links[i])
757
+ }
758
+ }
759
+ if !bytes.Equal(this.Data, that1.Data) {
760
+ return fmt2.Errorf("Data this(%v) Not Equal that(%v)", this.Data, that1.Data)
761
+ }
762
+ if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) {
763
+ return fmt2.Errorf("XXX_unrecognized this(%v) Not Equal that(%v)", this.XXX_unrecognized, that1.XXX_unrecognized)
764
+ }
765
+ return nil
766
+}
767
+func (this *PBNode) Equal(that interface{}) bool {
768
+ if that == nil {
769
+ if this == nil {
770
+ return true
771
+ }
772
+ return false
773
+ }
774
+
775
+ that1, ok := that.(*PBNode)
776
+ if !ok {
777
+ return false
778
+ }
779
+ if that1 == nil {
780
+ if this == nil {
781
+ return true
782
+ }
783
+ return false
784
+ } else if this == nil {
785
+ return false
786
+ }
787
+ if len(this.Links) != len(that1.Links) {
788
+ return false
789
+ }
790
+ for i := range this.Links {
791
+ if !this.Links[i].Equal(that1.Links[i]) {
792
+ return false
793
+ }
794
+ }
795
+ if !bytes.Equal(this.Data, that1.Data) {
796
+ return false
797
+ }
798
+ if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) {
799
+ return false
800
+ }
801
+ return true
802
+}
merkledag/nodepb_test.go
new
+472
@@ -0,0 +1,472 @@
1
+// Code generated by protoc-gen-gogo.
2
+// source: node.proto
3
+// DO NOT EDIT!
4
+
5
+/*
6
+Package merkledag is a generated protocol buffer package.
7
+
8
+It is generated from these files:
9
+ node.proto
10
+
11
+It has these top-level messages:
12
+ PBLink
13
+ PBNode
14
+*/
15
+package merkledag
16
+
17
+import testing "testing"
18
+import math_rand "math/rand"
19
+import time "time"
20
+import code_google_com_p_gogoprotobuf_proto "code.google.com/p/gogoprotobuf/proto"
21
+import testing1 "testing"
22
+import math_rand1 "math/rand"
23
+import time1 "time"
24
+import encoding_json "encoding/json"
25
+import testing2 "testing"
26
+import math_rand2 "math/rand"
27
+import time2 "time"
28
+import code_google_com_p_gogoprotobuf_proto1 "code.google.com/p/gogoprotobuf/proto"
29
+import math_rand3 "math/rand"
30
+import time3 "time"
31
+import testing3 "testing"
32
+import fmt "fmt"
33
+import math_rand4 "math/rand"
34
+import time4 "time"
35
+import testing4 "testing"
36
+import code_google_com_p_gogoprotobuf_proto2 "code.google.com/p/gogoprotobuf/proto"
37
+import math_rand5 "math/rand"
38
+import time5 "time"
39
+import testing5 "testing"
40
+import fmt1 "fmt"
41
+import go_parser "go/parser"
42
+import math_rand6 "math/rand"
43
+import time6 "time"
44
+import testing6 "testing"
45
+import code_google_com_p_gogoprotobuf_proto3 "code.google.com/p/gogoprotobuf/proto"
46
+
47
+func TestPBLinkProto(t *testing.T) {
48
+ popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
49
+ p := NewPopulatedPBLink(popr, false)
50
+ data, err := code_google_com_p_gogoprotobuf_proto.Marshal(p)
51
+ if err != nil {
52
+ panic(err)
53
+ }
54
+ msg := &PBLink{}
55
+ if err := code_google_com_p_gogoprotobuf_proto.Unmarshal(data, msg); err != nil {
56
+ panic(err)
57
+ }
58
+ for i := range data {
59
+ data[i] = byte(popr.Intn(256))
60
+ }
61
+ if err := p.VerboseEqual(msg); err != nil {
62
+ t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err)
63
+ }
64
+ if !p.Equal(msg) {
65
+ t.Fatalf("%#v !Proto %#v", msg, p)
66
+ }
67
+}
68
+
69
+func TestPBLinkMarshalTo(t *testing.T) {
70
+ popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
71
+ p := NewPopulatedPBLink(popr, false)
72
+ size := p.Size()
73
+ data := make([]byte, size)
74
+ for i := range data {
75
+ data[i] = byte(popr.Intn(256))
76
+ }
77
+ _, err := p.MarshalTo(data)
78
+ if err != nil {
79
+ panic(err)
80
+ }
81
+ msg := &PBLink{}
82
+ if err := code_google_com_p_gogoprotobuf_proto.Unmarshal(data, msg); err != nil {
83
+ panic(err)
84
+ }
85
+ for i := range data {
86
+ data[i] = byte(popr.Intn(256))
87
+ }
88
+ if err := p.VerboseEqual(msg); err != nil {
89
+ t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err)
90
+ }
91
+ if !p.Equal(msg) {
92
+ t.Fatalf("%#v !Proto %#v", msg, p)
93
+ }
94
+}
95
+
96
+func BenchmarkPBLinkProtoMarshal(b *testing.B) {
97
+ popr := math_rand.New(math_rand.NewSource(616))
98
+ total := 0
99
+ pops := make([]*PBLink, 10000)
100
+ for i := 0; i < 10000; i++ {
101
+ pops[i] = NewPopulatedPBLink(popr, false)
102
+ }
103
+ b.ResetTimer()
104
+ for i := 0; i < b.N; i++ {
105
+ data, err := code_google_com_p_gogoprotobuf_proto.Marshal(pops[i%10000])
106
+ if err != nil {
107
+ panic(err)
108
+ }
109
+ total += len(data)
110
+ }
111
+ b.SetBytes(int64(total / b.N))
112
+}
113
+
114
+func BenchmarkPBLinkProtoUnmarshal(b *testing.B) {
115
+ popr := math_rand.New(math_rand.NewSource(616))
116
+ total := 0
117
+ datas := make([][]byte, 10000)
118
+ for i := 0; i < 10000; i++ {
119
+ data, err := code_google_com_p_gogoprotobuf_proto.Marshal(NewPopulatedPBLink(popr, false))
120
+ if err != nil {
121
+ panic(err)
122
+ }
123
+ datas[i] = data
124
+ }
125
+ msg := &PBLink{}
126
+ b.ResetTimer()
127
+ for i := 0; i < b.N; i++ {
128
+ total += len(datas[i%10000])
129
+ if err := code_google_com_p_gogoprotobuf_proto.Unmarshal(datas[i%10000], msg); err != nil {
130
+ panic(err)
131
+ }
132
+ }
133
+ b.SetBytes(int64(total / b.N))
134
+}
135
+
136
+func TestPBNodeProto(t *testing.T) {
137
+ popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
138
+ p := NewPopulatedPBNode(popr, false)
139
+ data, err := code_google_com_p_gogoprotobuf_proto.Marshal(p)
140
+ if err != nil {
141
+ panic(err)
142
+ }
143
+ msg := &PBNode{}
144
+ if err := code_google_com_p_gogoprotobuf_proto.Unmarshal(data, msg); err != nil {
145
+ panic(err)
146
+ }
147
+ for i := range data {
148
+ data[i] = byte(popr.Intn(256))
149
+ }
150
+ if err := p.VerboseEqual(msg); err != nil {
151
+ t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err)
152
+ }
153
+ if !p.Equal(msg) {
154
+ t.Fatalf("%#v !Proto %#v", msg, p)
155
+ }
156
+}
157
+
158
+func TestPBNodeMarshalTo(t *testing.T) {
159
+ popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
160
+ p := NewPopulatedPBNode(popr, false)
161
+ size := p.Size()
162
+ data := make([]byte, size)
163
+ for i := range data {
164
+ data[i] = byte(popr.Intn(256))
165
+ }
166
+ _, err := p.MarshalTo(data)
167
+ if err != nil {
168
+ panic(err)
169
+ }
170
+ msg := &PBNode{}
171
+ if err := code_google_com_p_gogoprotobuf_proto.Unmarshal(data, msg); err != nil {
172
+ panic(err)
173
+ }
174
+ for i := range data {
175
+ data[i] = byte(popr.Intn(256))
176
+ }
177
+ if err := p.VerboseEqual(msg); err != nil {
178
+ t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err)
179
+ }
180
+ if !p.Equal(msg) {
181
+ t.Fatalf("%#v !Proto %#v", msg, p)
182
+ }
183
+}
184
+
185
+func BenchmarkPBNodeProtoMarshal(b *testing.B) {
186
+ popr := math_rand.New(math_rand.NewSource(616))
187
+ total := 0
188
+ pops := make([]*PBNode, 10000)
189
+ for i := 0; i < 10000; i++ {
190
+ pops[i] = NewPopulatedPBNode(popr, false)
191
+ }
192
+ b.ResetTimer()
193
+ for i := 0; i < b.N; i++ {
194
+ data, err := code_google_com_p_gogoprotobuf_proto.Marshal(pops[i%10000])
195
+ if err != nil {
196
+ panic(err)
197
+ }
198
+ total += len(data)
199
+ }
200
+ b.SetBytes(int64(total / b.N))
201
+}
202
+
203
+func BenchmarkPBNodeProtoUnmarshal(b *testing.B) {
204
+ popr := math_rand.New(math_rand.NewSource(616))
205
+ total := 0
206
+ datas := make([][]byte, 10000)
207
+ for i := 0; i < 10000; i++ {
208
+ data, err := code_google_com_p_gogoprotobuf_proto.Marshal(NewPopulatedPBNode(popr, false))
209
+ if err != nil {
210
+ panic(err)
211
+ }
212
+ datas[i] = data
213
+ }
214
+ msg := &PBNode{}
215
+ b.ResetTimer()
216
+ for i := 0; i < b.N; i++ {
217
+ total += len(datas[i%10000])
218
+ if err := code_google_com_p_gogoprotobuf_proto.Unmarshal(datas[i%10000], msg); err != nil {
219
+ panic(err)
220
+ }
221
+ }
222
+ b.SetBytes(int64(total / b.N))
223
+}
224
+
225
+func TestPBLinkJSON(t *testing1.T) {
226
+ popr := math_rand1.New(math_rand1.NewSource(time1.Now().UnixNano()))
227
+ p := NewPopulatedPBLink(popr, true)
228
+ jsondata, err := encoding_json.Marshal(p)
229
+ if err != nil {
230
+ panic(err)
231
+ }
232
+ msg := &PBLink{}
233
+ err = encoding_json.Unmarshal(jsondata, msg)
234
+ if err != nil {
235
+ panic(err)
236
+ }
237
+ if err := p.VerboseEqual(msg); err != nil {
238
+ t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err)
239
+ }
240
+ if !p.Equal(msg) {
241
+ t.Fatalf("%#v !Json Equal %#v", msg, p)
242
+ }
243
+}
244
+func TestPBNodeJSON(t *testing1.T) {
245
+ popr := math_rand1.New(math_rand1.NewSource(time1.Now().UnixNano()))
246
+ p := NewPopulatedPBNode(popr, true)
247
+ jsondata, err := encoding_json.Marshal(p)
248
+ if err != nil {
249
+ panic(err)
250
+ }
251
+ msg := &PBNode{}
252
+ err = encoding_json.Unmarshal(jsondata, msg)
253
+ if err != nil {
254
+ panic(err)
255
+ }
256
+ if err := p.VerboseEqual(msg); err != nil {
257
+ t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err)
258
+ }
259
+ if !p.Equal(msg) {
260
+ t.Fatalf("%#v !Json Equal %#v", msg, p)
261
+ }
262
+}
263
+func TestPBLinkProtoText(t *testing2.T) {
264
+ popr := math_rand2.New(math_rand2.NewSource(time2.Now().UnixNano()))
265
+ p := NewPopulatedPBLink(popr, true)
266
+ data := code_google_com_p_gogoprotobuf_proto1.MarshalTextString(p)
267
+ msg := &PBLink{}
268
+ if err := code_google_com_p_gogoprotobuf_proto1.UnmarshalText(data, msg); err != nil {
269
+ panic(err)
270
+ }
271
+ if err := p.VerboseEqual(msg); err != nil {
272
+ t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err)
273
+ }
274
+ if !p.Equal(msg) {
275
+ t.Fatalf("%#v !Proto %#v", msg, p)
276
+ }
277
+}
278
+
279
+func TestPBLinkProtoCompactText(t *testing2.T) {
280
+ popr := math_rand2.New(math_rand2.NewSource(time2.Now().UnixNano()))
281
+ p := NewPopulatedPBLink(popr, true)
282
+ data := code_google_com_p_gogoprotobuf_proto1.CompactTextString(p)
283
+ msg := &PBLink{}
284
+ if err := code_google_com_p_gogoprotobuf_proto1.UnmarshalText(data, msg); err != nil {
285
+ panic(err)
286
+ }
287
+ if err := p.VerboseEqual(msg); err != nil {
288
+ t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err)
289
+ }
290
+ if !p.Equal(msg) {
291
+ t.Fatalf("%#v !Proto %#v", msg, p)
292
+ }
293
+}
294
+
295
+func TestPBNodeProtoText(t *testing2.T) {
296
+ popr := math_rand2.New(math_rand2.NewSource(time2.Now().UnixNano()))
297
+ p := NewPopulatedPBNode(popr, true)
298
+ data := code_google_com_p_gogoprotobuf_proto1.MarshalTextString(p)
299
+ msg := &PBNode{}
300
+ if err := code_google_com_p_gogoprotobuf_proto1.UnmarshalText(data, msg); err != nil {
301
+ panic(err)
302
+ }
303
+ if err := p.VerboseEqual(msg); err != nil {
304
+ t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err)
305
+ }
306
+ if !p.Equal(msg) {
307
+ t.Fatalf("%#v !Proto %#v", msg, p)
308
+ }
309
+}
310
+
311
+func TestPBNodeProtoCompactText(t *testing2.T) {
312
+ popr := math_rand2.New(math_rand2.NewSource(time2.Now().UnixNano()))
313
+ p := NewPopulatedPBNode(popr, true)
314
+ data := code_google_com_p_gogoprotobuf_proto1.CompactTextString(p)
315
+ msg := &PBNode{}
316
+ if err := code_google_com_p_gogoprotobuf_proto1.UnmarshalText(data, msg); err != nil {
317
+ panic(err)
318
+ }
319
+ if err := p.VerboseEqual(msg); err != nil {
320
+ t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err)
321
+ }
322
+ if !p.Equal(msg) {
323
+ t.Fatalf("%#v !Proto %#v", msg, p)
324
+ }
325
+}
326
+
327
+func TestPBLinkStringer(t *testing3.T) {
328
+ popr := math_rand3.New(math_rand3.NewSource(time3.Now().UnixNano()))
329
+ p := NewPopulatedPBLink(popr, false)
330
+ s1 := p.String()
331
+ s2 := fmt.Sprintf("%v", p)
332
+ if s1 != s2 {
333
+ t.Fatalf("String want %v got %v", s1, s2)
334
+ }
335
+}
336
+func TestPBNodeStringer(t *testing3.T) {
337
+ popr := math_rand3.New(math_rand3.NewSource(time3.Now().UnixNano()))
338
+ p := NewPopulatedPBNode(popr, false)
339
+ s1 := p.String()
340
+ s2 := fmt.Sprintf("%v", p)
341
+ if s1 != s2 {
342
+ t.Fatalf("String want %v got %v", s1, s2)
343
+ }
344
+}
345
+func TestPBLinkSize(t *testing4.T) {
346
+ popr := math_rand4.New(math_rand4.NewSource(time4.Now().UnixNano()))
347
+ p := NewPopulatedPBLink(popr, true)
348
+ size2 := code_google_com_p_gogoprotobuf_proto2.Size(p)
349
+ data, err := code_google_com_p_gogoprotobuf_proto2.Marshal(p)
350
+ if err != nil {
351
+ panic(err)
352
+ }
353
+ size := p.Size()
354
+ if len(data) != size {
355
+ t.Fatalf("size %v != marshalled size %v", size, len(data))
356
+ }
357
+ if size2 != size {
358
+ t.Fatalf("size %v != before marshal proto.Size %v", size, size2)
359
+ }
360
+ size3 := code_google_com_p_gogoprotobuf_proto2.Size(p)
361
+ if size3 != size {
362
+ t.Fatalf("size %v != after marshal proto.Size %v", size, size3)
363
+ }
364
+}
365
+
366
+func BenchmarkPBLinkSize(b *testing4.B) {
367
+ popr := math_rand4.New(math_rand4.NewSource(616))
368
+ total := 0
369
+ pops := make([]*PBLink, 1000)
370
+ for i := 0; i < 1000; i++ {
371
+ pops[i] = NewPopulatedPBLink(popr, false)
372
+ }
373
+ b.ResetTimer()
374
+ for i := 0; i < b.N; i++ {
375
+ total += pops[i%1000].Size()
376
+ }
377
+ b.SetBytes(int64(total / b.N))
378
+}
379
+
380
+func TestPBNodeSize(t *testing4.T) {
381
+ popr := math_rand4.New(math_rand4.NewSource(time4.Now().UnixNano()))
382
+ p := NewPopulatedPBNode(popr, true)
383
+ size2 := code_google_com_p_gogoprotobuf_proto2.Size(p)
384
+ data, err := code_google_com_p_gogoprotobuf_proto2.Marshal(p)
385
+ if err != nil {
386
+ panic(err)
387
+ }
388
+ size := p.Size()
389
+ if len(data) != size {
390
+ t.Fatalf("size %v != marshalled size %v", size, len(data))
391
+ }
392
+ if size2 != size {
393
+ t.Fatalf("size %v != before marshal proto.Size %v", size, size2)
394
+ }
395
+ size3 := code_google_com_p_gogoprotobuf_proto2.Size(p)
396
+ if size3 != size {
397
+ t.Fatalf("size %v != after marshal proto.Size %v", size, size3)
398
+ }
399
+}
400
+
401
+func BenchmarkPBNodeSize(b *testing4.B) {
402
+ popr := math_rand4.New(math_rand4.NewSource(616))
403
+ total := 0
404
+ pops := make([]*PBNode, 1000)
405
+ for i := 0; i < 1000; i++ {
406
+ pops[i] = NewPopulatedPBNode(popr, false)
407
+ }
408
+ b.ResetTimer()
409
+ for i := 0; i < b.N; i++ {
410
+ total += pops[i%1000].Size()
411
+ }
412
+ b.SetBytes(int64(total / b.N))
413
+}
414
+
415
+func TestPBLinkGoString(t *testing5.T) {
416
+ popr := math_rand5.New(math_rand5.NewSource(time5.Now().UnixNano()))
417
+ p := NewPopulatedPBLink(popr, false)
418
+ s1 := p.GoString()
419
+ s2 := fmt1.Sprintf("%#v", p)
420
+ if s1 != s2 {
421
+ t.Fatalf("GoString want %v got %v", s1, s2)
422
+ }
423
+ _, err := go_parser.ParseExpr(s1)
424
+ if err != nil {
425
+ panic(err)
426
+ }
427
+}
428
+func TestPBNodeGoString(t *testing5.T) {
429
+ popr := math_rand5.New(math_rand5.NewSource(time5.Now().UnixNano()))
430
+ p := NewPopulatedPBNode(popr, false)
431
+ s1 := p.GoString()
432
+ s2 := fmt1.Sprintf("%#v", p)
433
+ if s1 != s2 {
434
+ t.Fatalf("GoString want %v got %v", s1, s2)
435
+ }
436
+ _, err := go_parser.ParseExpr(s1)
437
+ if err != nil {
438
+ panic(err)
439
+ }
440
+}
441
+func TestPBLinkVerboseEqual(t *testing6.T) {
442
+ popr := math_rand6.New(math_rand6.NewSource(time6.Now().UnixNano()))
443
+ p := NewPopulatedPBLink(popr, false)
444
+ data, err := code_google_com_p_gogoprotobuf_proto3.Marshal(p)
445
+ if err != nil {
446
+ panic(err)
447
+ }
448
+ msg := &PBLink{}
449
+ if err := code_google_com_p_gogoprotobuf_proto3.Unmarshal(data, msg); err != nil {
450
+ panic(err)
451
+ }
452
+ if err := p.VerboseEqual(msg); err != nil {
453
+ t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
454
+ }
455
+}
456
+func TestPBNodeVerboseEqual(t *testing6.T) {
457
+ popr := math_rand6.New(math_rand6.NewSource(time6.Now().UnixNano()))
458
+ p := NewPopulatedPBNode(popr, false)
459
+ data, err := code_google_com_p_gogoprotobuf_proto3.Marshal(p)
460
+ if err != nil {
461
+ panic(err)
462
+ }
463
+ msg := &PBNode{}
464
+ if err := code_google_com_p_gogoprotobuf_proto3.Unmarshal(data, msg); err != nil {
465
+ panic(err)
466
+ }
467
+ if err := p.VerboseEqual(msg); err != nil {
468
+ t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
469
+ }
470
+}
471
+
472
+//These tests are generated by code.google.com/p/gogoprotobuf/plugin/testgen