Skip to content

Commit

Permalink
Make code testable, add test against native xxd
Browse files Browse the repository at this point in the history
  • Loading branch information
felixge committed Jan 11, 2015
1 parent ff11c6f commit 90262b3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 18 deletions.
42 changes: 24 additions & 18 deletions xxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@ import (
"unicode/utf8"
)

const (
byteOffsetInit = 8
charOffsetInt = 39
line_length = 50
)

func main() {
line_offset := 0

if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s [file]\n", os.Args[0])
os.Exit(1)
Expand All @@ -27,8 +19,21 @@ func main() {
panic(err)
}
defer f.Close()
if err := XXD(f, os.Stdout); err != nil {
panic(err)
}
}

const (
byteOffsetInit = 8
charOffsetInt = 39
line_length = 50
)

func XXD(r io.Reader, w io.Writer) error {
line_offset := 0

r := bufio.NewReader(f)
r = bufio.NewReader(r)
buf := make([]byte, 16)
for {
n, err := r.Read(buf)
Expand All @@ -37,41 +42,42 @@ func main() {
}

// Line offset
fmt.Printf("%06x0: ", line_offset)
fmt.Fprintf(w, "%06x0: ", line_offset)
line_offset++

// Hex values
for i := 0; i < n; i++ {
fmt.Printf("%02x", buf[i])
fmt.Fprintf(w, "%02x", buf[i])

if i%2 == 1 {
fmt.Print(" ")
fmt.Fprint(w, " ")
}
}
if n < len(buf) {
for i := n; i < len(buf); i++ {
fmt.Printf(" ")
fmt.Fprintf(w, " ")
if i%2 == 1 {
fmt.Print(" ")
fmt.Fprint(w, " ")
}
}
}

fmt.Printf(" ")
fmt.Fprintf(w, " ")

// Character values
b := buf[:n]
for len(b) > 0 {
r, size := utf8.DecodeRune(b)

if int(r) > 0x1f && int(r) < 0x7f {
fmt.Printf("%v", string(r))
fmt.Fprintf(w, "%v", string(r))
} else {
fmt.Printf(".")
fmt.Fprintf(w, ".")
}
b = b[size:]
}

fmt.Printf("\n")
fmt.Fprintf(w, "\n")
}
return nil
}
54 changes: 54 additions & 0 deletions xxd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"bytes"
"flag"
"io"
"io/ioutil"
"os/exec"
"strings"
"testing"
"testing/quick"
)

var xxdFile = flag.String("xxdFile", "", "File to test against.")

func TestXXD(t *testing.T) {
if *xxdFile == "" {
t.Skip("-xxdFile argument not given")
}
data, err := ioutil.ReadFile(*xxdFile)
if err != nil {
t.Fatal(err)
}
test := func(fn func(r io.Reader, w io.Writer) error) func(n uint64) []string {
return func(n uint64) []string {
size := n % uint64(len(data))
var out bytes.Buffer
if err := fn(bytes.NewBuffer(data[0:size]), &out); err != nil {
return []string{err.Error()}
}
return strings.Split(out.String(), "\n")
}
}
if err := quick.CheckEqual(test(XXD), test(xxdNative), nil); err != nil {
cErr := err.(*quick.CheckEqualError)
size := cErr.In[0].(uint64) % uint64(len(data))
for i := range cErr.Out1[0].([]string) {
got := cErr.Out1[0].([]string)[i]
want := cErr.Out2[0].([]string)[i]
if got != want {
t.Errorf("size: %d\n\ngot : %s\nwant: %s\n", size, got, want)
break
}
}
}
}

func xxdNative(r io.Reader, w io.Writer) error {
xxd := exec.Command("xxd", "-")
xxd.Stdin = r
xxd.Stdout = w
xxd.Stderr = w
return xxd.Run()
}

0 comments on commit 90262b3

Please sign in to comment.