File: cw_test.go

package info (click to toggle)
golang-github-siddontang-go 0.0~git20170517.0.cb568a3-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 492 kB
  • sloc: makefile: 3
file content (72 lines) | stat: -rw-r--r-- 1,686 bytes parent folder | download | duplicates (2)
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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package bytes2 gives you alternate implementations of functionality
// similar to go's bytes package

package bytes2

import (
	"testing"
)

func TestWrite(t *testing.T) {
	cw := NewChunkedWriter(5)
	cw.Write([]byte("1234"))
	if string(cw.Bytes()) != "1234" {
		t.Errorf("Expecting 1234, received %s", cw.Bytes())
	}
	cw.WriteString("56")
	if string(cw.Bytes()) != "123456" {
		t.Errorf("Expecting 123456, received %s", cw.Bytes())
	}
	if cw.Len() != 6 {
		t.Errorf("Expecting 6, received %d", cw.Len())
	}
}

func TestTruncate(t *testing.T) {
	cw := NewChunkedWriter(3)
	cw.WriteString("123456789")
	cw.Truncate(8)
	if string(cw.Bytes()) != "12345678" {
		t.Errorf("Expecting 12345678, received %s", cw.Bytes())
	}
	cw.Truncate(5)
	if string(cw.Bytes()) != "12345" {
		t.Errorf("Expecting 12345, received %s", cw.Bytes())
	}
	cw.Truncate(2)
	if string(cw.Bytes()) != "12" {
		t.Errorf("Expecting 12345, received %s", cw.Bytes())
	}
	cw.Reset()
	if cw.Len() != 0 {
		t.Errorf("Expecting 0, received %d", cw.Len())
	}
}

func TestReserve(t *testing.T) {
	cw := NewChunkedWriter(4)
	b := cw.Reserve(2)
	b[0] = '1'
	b[1] = '2'
	cw.WriteByte('3')
	b = cw.Reserve(2)
	b[0] = '4'
	b[1] = '5'
	if string(cw.Bytes()) != "12345" {
		t.Errorf("Expecting 12345, received %s", cw.Bytes())
	}
}

func TestWriteTo(t *testing.T) {
	cw1 := NewChunkedWriter(4)
	cw1.WriteString("123456789")
	cw2 := NewChunkedWriter(5)
	cw1.WriteTo(cw2)
	if string(cw2.Bytes()) != "123456789" {
		t.Errorf("Expecting 123456789, received %s", cw2.Bytes())
	}
}