Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

render_example_test.gno

2.44 Kb · 70 lines
 1package varintdemo
 2
 3// ExampleRender pins the realm's Render output as a testable example. The
 4// unsigned column is the protobuf base-128 vector set, so this doubles as a
 5// conformance test against the library.
 6func ExampleRender() {
 7	print(Render(""))
 8	// Output:
 9	// # Varint
10	//
11	// LEB128 base-128 integers, demoing the [`p/moul/x/daily/varint`](/p/moul/x/daily/varint/v0) library.
12	//
13	// ## Unsigned
14	//
15	// Seven payload bits per byte, so the length steps at every power of 128.
16	//
17	// | value | encoding | bytes |
18	// | --- | --- | --- |
19	// | `0` | `00` | 1 |
20	// | `1` | `01` | 1 |
21	// | `127` | `7f` | 1 |
22	// | `128` | `80 01` | 2 |
23	// | `300` | `ac 02` | 2 |
24	// | `16383` | `ff 7f` | 2 |
25	// | `16384` | `80 80 01` | 3 |
26	// | `4294967296` | `80 80 80 80 10` | 5 |
27	// | `18446744073709551615` | `ff ff ff ff ff ff ff ff ff 01` | 10 |
28	//
29	// ## Signed, through zigzag
30	//
31	// Encoding a negative directly would sign-extend it to ten bytes. Zigzag interleaves the signs first, so `-1` costs one byte and not ten.
32	//
33	// | value | zigzag | encoding | bytes |
34	// | --- | --- | --- | --- |
35	// | `0` | `0` | `00` | 1 |
36	// | `-1` | `1` | `01` | 1 |
37	// | `1` | `2` | `02` | 1 |
38	// | `-64` | `127` | `7f` | 1 |
39	// | `64` | `128` | `80 01` | 2 |
40	// | `-9223372036854775808` | `18446744073709551615` | `ff ff ff ff ff ff ff ff ff 01` | 10 |
41	// | `9223372036854775807` | `18446744073709551614` | `fe ff ff ff ff ff ff ff ff 01` | 10 |
42	//
43	// ## Only the canonical encoding decodes
44	//
45	// A padded varint means the same number has two spellings, which is a different hash for the same state. The decoder refuses them.
46	//
47	// | input | result |
48	// | --- | --- |
49	// | `80 00`, zero with one padding group | `varint: non-canonical encoding` |
50	// | `81 00`, one with one padding group | `varint: non-canonical encoding` |
51	// | `80`, continuation bit then nothing | `varint: truncated` |
52	// | `ff ... ff 02`, ten groups, eleven bits too many | `varint: value overflows uint64` |
53	//
54	// > Try another value: append it to the path, for instance [`/-300`](/r/moul/x/daily/varintdemo/v0:/-300).
55}
56
57// ExampleRender_one pins the single-value path, and with it the escaping the
58// realm owes a caller-supplied argument.
59func ExampleRender_one() {
60	print(Render("/-300"))
61	// Output:
62	// # Varint
63	//
64	// LEB128 base-128 integers, demoing the [`p/moul/x/daily/varint`](/p/moul/x/daily/varint/v0) library.
65	//
66	// ## `-300`
67	//
68	// - zigzag: `599`
69	// - signed: `d7 04` (2 bytes)
70}