Have zlibcut example use log.Fatalf

This commit is contained in:
Nigel Tao committed 2019-08-08 15:46:50 +10:00
1 parent d070c6ad47
commit 4fd8ead21f
1 file changed
+11 -20
+11 -20
View File
@@ -19,6 +19,7 @@ import (
"compress/zlib" "compress/zlib"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"strings" "strings"
"github.com/google/wuffs/lib/zlibcut" "github.com/google/wuffs/lib/zlibcut"
@@ -42,8 +43,7 @@ func ExampleCut() {
"So long lives this, and this gives life to thee.\n" "So long lives this, and this gives life to thee.\n"
if n := len(sonnet18); n != 632 { if n := len(sonnet18); n != 632 {
fmt.Printf("len(sonnet18): got %d, want 632", n) log.Fatalf("len(sonnet18): got %d, want 632", n)
return
} }
// Compress the input text, sonnet18. // Compress the input text, sonnet18.
@@ -58,22 +58,19 @@ func ExampleCut() {
// the Go standard library. Nonetheless, for a 632 byte input, we expect // the Go standard library. Nonetheless, for a 632 byte input, we expect
// the compressed form to be between 300 and 500 bytes. // the compressed form to be between 300 and 500 bytes.
if n := len(compressed); (n < 300) || (500 < n) { if n := len(compressed); (n < 300) || (500 < n) {
fmt.Printf("len(compressed): got %d, want something in [300, 500]", n) log.Fatalf("len(compressed): got %d, want something in [300, 500]", n)
return
} }
// Cut the 300-or-more bytes to be 200. // Cut the 300-or-more bytes to be 200.
encodedLen, decodedLen, err := zlibcut.Cut(nil, compressed, 200) encodedLen, decodedLen, err := zlibcut.Cut(nil, compressed, 200)
if err != nil { if err != nil {
fmt.Printf("Cut: %v", err) log.Fatalf("Cut: %v", err)
return
} }
// The encodedLen should be equal to or just under the requested 200. // The encodedLen should be equal to or just under the requested 200.
cut := compressed[:encodedLen] cut := compressed[:encodedLen]
if n := len(cut); (n < 190) || (200 < n) { if n := len(cut); (n < 190) || (200 < n) {
fmt.Printf("len(cut): got %d, want something in [190, 200]", n) log.Fatalf("len(cut): got %d, want something in [190, 200]", n)
return
} }
// At this point, a real program would write that cut slice somewhere. The // At this point, a real program would write that cut slice somewhere. The
@@ -84,18 +81,15 @@ func ExampleCut() {
// no errors should be encountered. // no errors should be encountered.
r, err := zlib.NewReader(bytes.NewReader(cut)) r, err := zlib.NewReader(bytes.NewReader(cut))
if err != nil { if err != nil {
fmt.Printf("NewReader: %v", err) log.Fatalf("NewReader: %v", err)
return
} }
uncompressed, err := ioutil.ReadAll(r) uncompressed, err := ioutil.ReadAll(r)
if err != nil { if err != nil {
fmt.Printf("ReadAll: %v", err) log.Fatalf("ReadAll: %v", err)
return
} }
err = r.Close() err = r.Close()
if err != nil { if err != nil {
fmt.Printf("Close: %v", err) log.Fatalf("Close: %v", err)
return
} }
// The uncompressed form of the cut data should be a prefix (of length // The uncompressed form of the cut data should be a prefix (of length
@@ -103,14 +97,11 @@ func ExampleCut() {
// depends on the zlib compression algorithm, but uncompressing 200 or so // depends on the zlib compression algorithm, but uncompressing 200 or so
// bytes should give between 250 and 400 bytes. // bytes should give between 250 and 400 bytes.
if n := len(uncompressed); n != decodedLen { if n := len(uncompressed); n != decodedLen {
fmt.Printf("len(uncompressed): got %d, want %d", n, decodedLen) log.Fatalf("len(uncompressed): got %d, want %d", n, decodedLen)
return
} else if (n < 250) || (400 < n) { } else if (n < 250) || (400 < n) {
fmt.Printf("len(uncompressed): got %d, want something in [250, 400]", n) log.Fatalf("len(uncompressed): got %d, want something in [250, 400]", n)
return
} else if !strings.HasPrefix(sonnet18, string(uncompressed)) { } else if !strings.HasPrefix(sonnet18, string(uncompressed)) {
fmt.Printf("uncompressed was not a prefix of the original input") log.Fatalf("uncompressed was not a prefix of the original input")
return
} }
// The first two lines of the sonnet take 83 bytes. // The first two lines of the sonnet take 83 bytes.