Skip to content

Commit

Permalink
Update to delimit deflated output with newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewfrench committed Dec 31, 2022
1 parent c9646ac commit d81db28
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 25 deletions.
3 changes: 3 additions & 0 deletions pkg/ghcomp/deflate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func TestDeflater_Deflate(t *testing.T) {
out = bytes.NewBuffer(nil)
err = Inflate(in, out)
assert.NoError(t, err)

inflatedElements := strings.Split(inflated, "\n")
assert.Equal(t, len(strings.Split(out.String(), "\n")), len(inflatedElements))
for _, e := range strings.Split(inflated, "\n") {
assert.Contains(t, out.String(), e)
}
Expand Down
23 changes: 1 addition & 22 deletions pkg/ghcomp/ghcomp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var (

const (
GlobalStart = '!'
SegmentStop = '.'
SegmentStop = '\n'
)

// Tree is the receiver for publicly exported methods.
Expand Down Expand Up @@ -57,7 +57,6 @@ func (t *Tree) Entree(value []byte) error {
// EntreeDeflated adds deflated geohash values to the tree.
func (t *Tree) EntreeDeflated(in io.Reader) error {
scanner := bufio.NewScanner(in)
scanner.Split(ScanSegment)
if !scanner.Scan() {
return fmt.Errorf("failed to scan input source")
}
Expand All @@ -83,23 +82,3 @@ func (t *Tree) EntreeDeflated(in io.Reader) error {

return nil
}

// ScanSegment reads a chunk of deflated geohash data delimited by SegmentStop.
var ScanSegment bufio.SplitFunc = func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF {
return
}

advance = 0
token = make([]byte, 0)
for i := range data {
advance++
if data[i] == SegmentStop {
return
}

token = append(token, data[i])
}

return
}
2 changes: 1 addition & 1 deletion pkg/ghcomp/ghcomp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
const (
precision = 12
inflated = "bdvkhunfnc90\nbdvkj7vyvtz5\nbdvkjkjbpr1t\nbdvkjkjbremh\nbdvkjkn00jdn\n"
deflated = "bdvkhunfnc90.j7vyvtz5.kjbpr1t.remh.n00jdn."
deflated = "bdvkhunfnc90\nj7vyvtz5\nkjbpr1t\nremh\nn00jdn\n"
)

func TestTree_Entree(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion pkg/ghcomp/inflate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
// can inflate the deflated values directly without using a tree.
func Inflate(in io.Reader, out io.Writer) error {
scanner := bufio.NewScanner(in)
scanner.Split(ScanSegment)
if !scanner.Scan() {
return fmt.Errorf("failed to scan input source")
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/ghcomp/inflate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ func TestInflater_Inflate(t *testing.T) {

err := Inflate(in, out)
assert.NoError(t, err)
for _, e := range strings.Split(inflated, "\n") {

inflatedElements := strings.Split(inflated, "\n")
assert.Equal(t, len(strings.Split(out.String(), "\n")), len(inflatedElements))
for _, e := range inflatedElements {
assert.Contains(t, out.String(), e)
}
}

0 comments on commit d81db28

Please sign in to comment.