aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAxel Wagner <axel.wagner.hh@googlemail.com>2017-07-15 11:43:22 -0600
committerBrad Fitzpatrick <bradfitz@golang.org>2017-08-03 21:00:45 +0000
commit0173631d53df98b17d282b3d71e8f515388915cc (patch)
treed8f48f0f1189db718df3bb44f33ec677e55ed6f7
parentac0ccf3cd2464c6df3193ad8aec8d6053000cdb5 (diff)
downloadgo-0173631d53df98b17d282b3d71e8f515388915cc.tar.gz
go-0173631d53df98b17d282b3d71e8f515388915cc.zip
encoding/binary: add examples for varint functions
Change-Id: I191f6e46b452fadde9f641140445d843b0c7d534 Reviewed-on: https://go-review.googlesource.com/48604 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
-rw-r--r--src/encoding/binary/example_test.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/encoding/binary/example_test.go b/src/encoding/binary/example_test.go
index 2b52a47d12..a8b8dba650 100644
--- a/src/encoding/binary/example_test.go
+++ b/src/encoding/binary/example_test.go
@@ -68,3 +68,94 @@ func ExampleByteOrder_get() {
// Output:
// 0x03e8 0x07d0
}
+
+func ExamplePutUvarint() {
+ buf := make([]byte, binary.MaxVarintLen64)
+
+ for _, x := range []uint64{1, 2, 127, 128, 255, 256} {
+ n := binary.PutUvarint(buf, x)
+ fmt.Printf("%x\n", buf[:n])
+ }
+ // Output:
+ // 01
+ // 02
+ // 7f
+ // 8001
+ // ff01
+ // 8002
+}
+
+func ExamplePutVarint() {
+ buf := make([]byte, binary.MaxVarintLen64)
+
+ for _, x := range []int64{-65, -64, -2, -1, 0, 1, 2, 63, 64} {
+ n := binary.PutVarint(buf, x)
+ fmt.Printf("%x\n", buf[:n])
+ }
+ // Output:
+ // 8101
+ // 7f
+ // 03
+ // 01
+ // 00
+ // 02
+ // 04
+ // 7e
+ // 8001
+}
+
+func ExampleUvarint() {
+ inputs := [][]byte{
+ []byte{0x01},
+ []byte{0x02},
+ []byte{0x7f},
+ []byte{0x80, 0x01},
+ []byte{0xff, 0x01},
+ []byte{0x80, 0x02},
+ }
+ for _, b := range inputs {
+ x, n := binary.Uvarint(b)
+ if n != len(b) {
+ fmt.Println("Uvarint did not consume all of in")
+ }
+ fmt.Println(x)
+ }
+ // Output:
+ // 1
+ // 2
+ // 127
+ // 128
+ // 255
+ // 256
+}
+
+func ExampleVarint() {
+ inputs := [][]byte{
+ []byte{0x81, 0x01},
+ []byte{0x7f},
+ []byte{0x03},
+ []byte{0x01},
+ []byte{0x00},
+ []byte{0x02},
+ []byte{0x04},
+ []byte{0x7e},
+ []byte{0x80, 0x01},
+ }
+ for _, b := range inputs {
+ x, n := binary.Varint(b)
+ if n != len(b) {
+ fmt.Println("Varint did not consume all of in")
+ }
+ fmt.Println(x)
+ }
+ // Output:
+ // -65
+ // -64
+ // -2
+ // -1
+ // 0
+ // 1
+ // 2
+ // 63
+ // 64
+}