blob: 76f4f4f8da37b55d143420641ba800bc61a78d57 [file] [log] [blame]
// Copyright 2019 The Wuffs Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package zlibcut_test
import (
func ExampleCut() {
const sonnet18 = "" +
"Shall I compare thee to a summer’s day?\n" +
"Thou art more lovely and more temperate.\n" +
"Rough winds do shake the darling buds of May,\n" +
"And summer’s lease hath all too short a date.\n" +
"Sometime too hot the eye of heaven shines,\n" +
"And often is his gold complexion dimmed;\n" +
"And every fair from fair sometime declines,\n" +
"By chance, or nature’s changing course, untrimmed;\n" +
"But thy eternal summer shall not fade,\n" +
"Nor lose possession of that fair thou ow’st,\n" +
"Nor shall death brag thou wand'rest in his shade,\n" +
"When in eternal lines to Time thou grow'st.\n" +
"So long as men can breathe, or eyes can see,\n" +
"So long lives this, and this gives life to thee.\n"
if n := len(sonnet18); n != 632 {
fmt.Printf("len(sonnet18): got %d, want 632", n)
// Compress the input text, sonnet18.
buffer := &bytes.Buffer{}
w := zlib.NewWriter(buffer)
compressed := buffer.Bytes()
// The exact length of the zlib-compressed form of sonnet18 depends on the
// compression algorithm used, which can change from version to version of
// the Go standard library. Nonetheless, for a 632 byte input, we expect
// the compressed form to be between 300 and 500 bytes.
if n := len(compressed); (n < 300) || (500 < n) {
fmt.Printf("len(compressed): got %d, want something in [300, 500]", n)
// Cut the 300-or-more bytes to be 200.
encodedLen, decodedLen, err := zlibcut.Cut(nil, compressed, 200)
if err != nil {
fmt.Printf("Cut: %v", err)
// The encodedLen should be equal to or just under the requested 200.
cut := compressed[:encodedLen]
if n := len(cut); (n < 190) || (200 < n) {
fmt.Printf("len(cut): got %d, want something in [190, 200]", n)
// At this point, a real program would write that cut slice somewhere. The
// rest of this example verifies that the cut data has the properties we
// expect, given the semantics of zlibcut.Cut.
// Uncompress the cut data. It should be a valid zlib-compressed stream, so
// no errors should be encountered.
r, err := zlib.NewReader(bytes.NewReader(cut))
if err != nil {
fmt.Printf("NewReader: %v", err)
uncompressed, err := ioutil.ReadAll(r)
if err != nil {
fmt.Printf("ReadAll: %v", err)
err = r.Close()
if err != nil {
fmt.Printf("Close: %v", err)
// The uncompressed form of the cut data should be a prefix (of length
// decodedLen) of the original input, sonnet18. Again, the exact length
// depends on the zlib compression algorithm, but uncompressing 200 or so
// bytes should give between 250 and 400 bytes.
if n := len(uncompressed); n != decodedLen {
fmt.Printf("len(uncompressed): got %d, want %d", n, decodedLen)
} else if (n < 250) || (400 < n) {
fmt.Printf("len(uncompressed): got %d, want something in [250, 400]", n)
} else if !strings.HasPrefix(sonnet18, string(uncompressed)) {
fmt.Printf("uncompressed was not a prefix of the original input")
// The first two lines of the sonnet take 83 bytes.
// Output:
// Shall I compare thee to a summer’s day?
// Thou art more lovely and more temperate.