summaryrefslogtreecommitdiff
path: root/methods_test.go
blob: 3a5254f23a0278f28827e24ffb5d29ee0bcd3feb (plain)
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
121
122
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package lowmemjson_test

import (
	"bytes"
	"fmt"
	"io"
	"testing"

	"github.com/stretchr/testify/assert"

	"git.lukeshu.com/go/lowmemjson"
)

type ShortSum string

func (s ShortSum) EncodeJSON(w io.Writer) error {
	// Test that it's OK to call lowmemjson.Encoder.Encode for the top-level value in a method.
	return lowmemjson.NewEncoder(w).Encode(string(s))
}

type SumRun struct {
	ChecksumSize int   `json:",omitempty"`
	Addr         int64 `json:",omitempty"`
	Sums         ShortSum
}

type SumRunWithGaps struct {
	Addr int64
	Size int64
	Runs []SumRun
}

func (sg SumRunWithGaps) EncodeJSON(w io.Writer) error {
	if _, err := fmt.Fprintf(w, `{"Addr":%d,"Size":%d,"Runs":[`, sg.Addr, sg.Size); err != nil {
		return err
	}
	cur := sg.Addr
	for i, run := range sg.Runs {
		if i > 0 {
			if _, err := w.Write([]byte{','}); err != nil {
				return err
			}
		}
		if run.Addr > cur {
			if _, err := fmt.Fprintf(w, `{"Gap":%d},`, run.Addr-cur); err != nil {
				return err
			}
		}
		if err := lowmemjson.NewEncoder(w).Encode(run); err != nil {
			return err
		}
	}
	end := sg.Addr + sg.Size
	switch {
	case end < cur:
		return fmt.Errorf("invalid %T: addr went backwards: %v < %v", sg, end, cur)
	case end > cur:
		if _, err := fmt.Fprintf(w, `,{"Gap":%d}`, end-cur); err != nil {
			return err
		}
	}
	if _, err := w.Write([]byte("]}")); err != nil {
		return err
	}
	return nil
}

func (sg *SumRunWithGaps) DecodeJSON(r io.RuneScanner) error {
	*sg = SumRunWithGaps{}
	var name string
	return lowmemjson.DecodeObject(r,
		func(r io.RuneScanner) error {
			return lowmemjson.NewDecoder(r).Decode(&name)
		},
		func(r io.RuneScanner) error {
			switch name {
			case "Addr":
				return lowmemjson.NewDecoder(r).Decode(&sg.Addr)
			case "Size":
				return lowmemjson.NewDecoder(r).Decode(&sg.Size)
			case "Runs":
				return lowmemjson.DecodeArray(r, func(r io.RuneScanner) error {
					var run SumRun
					if err := lowmemjson.NewDecoder(r).Decode(&run); err != nil {
						return err
					}
					if run.ChecksumSize > 0 {
						sg.Runs = append(sg.Runs, run)
					}
					return nil
				})
			default:
				return fmt.Errorf("unknown key %q", name)
			}
		})
}

func TestMethods(t *testing.T) {
	in := SumRunWithGaps{
		Addr: 13631488,
		Size: 416033783808,
		Runs: []SumRun{
			{
				ChecksumSize: 4,
				Addr:         1095761920,
				Sums:         "c160817cb5c72bbbe",
			},
		},
	}
	var buf bytes.Buffer
	assert.NoError(t, lowmemjson.NewEncoder(&buf).Encode(in))
	assert.Equal(t,
		`{"Addr":13631488,"Size":416033783808,"Runs":[{"Gap":1082130432},{"ChecksumSize":4,"Addr":1095761920,"Sums":"c160817cb5c72bbbe"},{"Gap":416033783808}]}`,
		buf.String())
	var out SumRunWithGaps
	assert.NoError(t, lowmemjson.NewDecoder(&buf).Decode(&out))
	assert.Equal(t, in, out)
}