1
0
mirror of https://github.com/golang/go synced 2024-11-25 04:57:56 -07:00

another round of gofmt applications

R=gri
DELTA=900  (106 added, 31 deleted, 763 changed)
OCL=35384
CL=35396
This commit is contained in:
Russ Cox 2009-10-06 14:55:39 -07:00
parent 22c98a3314
commit 650bff6aa9
37 changed files with 828 additions and 753 deletions

View File

@ -18,9 +18,9 @@ func rot13(b byte) byte {
b = 'a' + ((b-'a')+13)%26;
}
if 'A' <= b && b <= 'Z' {
b = 'A' + ((b - 'A') + 13) % 26
b = 'A' + ((b-'A')+13)%26;
}
return b
return b;
}
type reader interface {
@ -33,19 +33,19 @@ type rotate13 struct {
}
func newRotate13(source reader) *rotate13 {
return &rotate13{source}
return &rotate13{source};
}
func (r13 *rotate13) Read(b []byte) (ret int, err os.Error) {
r, e := r13.source.Read(b);
for i := 0; i < r; i++ {
b[i] = rot13(b[i])
b[i] = rot13(b[i]);
}
return r, e
return r, e;
}
func (r13 *rotate13) String() string {
return r13.source.String()
return r13.source.String();
}
// end of rotate13 implementation
@ -54,7 +54,7 @@ func cat(r reader) {
var buf [NBUF]byte;
if *rot13_flag {
r = newRotate13(r)
r = newRotate13(r);
}
for {
switch nr, er := r.Read(&buf); {

View File

@ -5,6 +5,7 @@
package PACKAGE
type Pointer *any
func Offsetof(any) int
func Sizeof(any) int
func Alignof(any) int

View File

@ -67,6 +67,7 @@ func checksum(header []byte) (unsigned int64, signed int64) {
}
type slicer []byte
func (sp *slicer) next(n int) (b []byte) {
s := *sp;
b, *sp = s[0:n], s[n:len(s)];

View File

@ -87,6 +87,7 @@ func (tr *Reader) octal(b []byte) int64 {
}
type ignoreWriter struct{}
func (ignoreWriter) Write(b []byte) (n int, err os.Error) {
return len(b), nil;
}

View File

@ -108,7 +108,7 @@ testLoop:
f, err := os.Open(test.file, os.O_RDONLY, 0444);
if err != nil {
t.Errorf("test %d: Unexpected error: %v", i, err);
continue
continue;
}
tr := NewReader(f);
for j, header := range test.headers {
@ -116,7 +116,7 @@ testLoop:
if err != nil || hdr == nil {
t.Errorf("test %d, entry %d: Didn't get entry: %v", i, j, err);
f.Close();
continue testLoop
continue testLoop;
}
if !reflect.DeepEqual(hdr, header) {
t.Errorf("test %d, entry %d: Incorrect header:\nhave %+v\nwant %+v",

View File

@ -46,7 +46,7 @@ type Writer struct {
// NewWriter creates a new Writer writing to w.
func NewWriter(w io.Writer) *Writer {
return &Writer{ w: w }
return &Writer{w: w};
}
// Flush finishes writing the current file (optional).
@ -63,7 +63,7 @@ func (tw *Writer) Flush() os.Error {
}
tw.nb = 0;
tw.pad = 0;
return tw.err
return tw.err;
}
// Write s into b, terminating it with a NUL if there is room.
@ -72,7 +72,7 @@ func (tw *Writer) cString(b []byte, s string) {
if tw.err == nil {
tw.err = ErrFieldTooLong;
}
return
return;
}
for i, ch := range strings.Bytes(s) {
b[i] = ch;
@ -98,7 +98,7 @@ func (tw *Writer) numeric(b []byte, x int64) {
s := strconv.Itob64(x, 8);
if len(s) < len(b) {
tw.octal(b, x);
return
return;
}
// Too big: use binary (big-endian).
tw.usedBinary = true;
@ -116,7 +116,7 @@ func (tw *Writer) WriteHeader(hdr *Header) os.Error {
tw.Flush();
}
if tw.err != nil {
return tw.err
return tw.err;
}
tw.nb = int64(hdr.Size);
@ -155,12 +155,12 @@ func (tw *Writer) WriteHeader(hdr *Header) os.Error {
if tw.err != nil {
// problem with header; probably integer too big for a field.
return tw.err
return tw.err;
}
_, tw.err = tw.w.Write(header);
return tw.err
return tw.err;
}
// Write writes to the current entry in the tar archive.
@ -178,12 +178,12 @@ func (tw *Writer) Write(b []uint8) (n int, err os.Error) {
err = ErrWriteTooLong;
}
tw.err = err;
return
return;
}
func (tw *Writer) Close() os.Error {
if tw.err != nil || tw.closed {
return tw.err
return tw.err;
}
tw.Flush();
tw.closed = true;
@ -192,8 +192,8 @@ func (tw *Writer) Close() os.Error {
for i := 0; i < 2; i++ {
_, tw.err = tw.w.Write(zeroBlock);
if tw.err != nil {
break
break;
}
}
return tw.err
return tw.err;
}

View File

@ -45,11 +45,11 @@ func NewEncoding(encoder string) *Encoding {
// StdEncoding is the standard base64 encoding, as defined in
// RFC 4648.
var StdEncoding = NewEncoding(encodeStd);
var StdEncoding = NewEncoding(encodeStd)
// URLEncoding is the alternate base64 encoding defined in RFC 4648.
// It is typically used in URLs and file names.
var URLEncoding = NewEncoding(encodeURL);
var URLEncoding = NewEncoding(encodeURL)
/*
* Encoder
@ -200,7 +200,7 @@ func (enc *Encoding) EncodedLen(n int) int {
* Decoder
*/
type CorruptInputError int64;
type CorruptInputError int64
func (e CorruptInputError) String() string {
return "illegal base64 data at input byte" + strconv.Itoa64(int64(e));

View File

@ -14,7 +14,9 @@ func newZ(x int64) *Int {
type funZZ func(z, x, y *Int) *Int
type argZZ struct { z, x, y *Int }
type argZZ struct {
z, x, y *Int;
}
var sumZZ = []argZZ{
argZZ{newZ(0), newZ(0), newZ(0)},
@ -55,8 +57,12 @@ func testFunZZ(t *testing.T, msg string, f funZZ, a argZZ) {
func TestSumZZ(t *testing.T) {
AddZZ := func(z, x, y *Int) *Int { return z.Add(x, y) };
SubZZ := func(z, x, y *Int) *Int { return z.Sub(x, y) };
AddZZ := func(z, x, y *Int) *Int {
return z.Add(x, y);
};
SubZZ := func(z, x, y *Int) *Int {
return z.Sub(x, y);
};
for _, a := range sumZZ {
arg := a;
testFunZZ(t, "AddZZ", AddZZ, arg);
@ -74,7 +80,9 @@ func TestSumZZ(t *testing.T) {
func TestProdZZ(t *testing.T) {
MulZZ := func(z, x, y *Int) *Int { return z.Mul(x, y) };
MulZZ := func(z, x, y *Int) *Int {
return z.Mul(x, y);
};
for _, a := range prodZZ {
arg := a;
testFunZZ(t, "MulZZ", MulZZ, arg);

View File

@ -85,6 +85,7 @@ type strN struct {
b int;
s string;
}
var tabN = []strN{
strN{nil, 10, "0"},
strN{[]Word{1}, 10, "1"},

View File

@ -25,7 +25,7 @@ func Mul128(x, y uint64) (z1, z0 uint64) {
)
if x < y {
x, y = y, x
x, y = y, x;
}
if x < B2 {

View File

@ -110,7 +110,7 @@ func dump(x Natural) {
// Natural represents an unsigned integer value of arbitrary precision.
//
type Natural []digit;
type Natural []digit
// Nat creates a small natural number with value x.
@ -152,8 +152,10 @@ func (x Natural) Value() uint64 {
// single-digit values
n := len(x);
switch n {
case 0: return 0;
case 1: return uint64(x[0]);
case 0:
return 0;
case 1:
return uint64(x[0]);
}
// multi-digit values
@ -204,7 +206,9 @@ func (x Natural) IsZero() bool {
func normalize(x Natural) Natural {
n := len(x);
for n > 0 && x[n-1] == 0 { n-- }
for n > 0 && x[n-1] == 0 {
n--;
}
return x[0:n];
}
@ -253,7 +257,7 @@ func Nadd(zp *Natural, x, y Natural) {
z[i] = c;
i++;
}
*zp = z[0 : i]
*zp = z[0:i];
}
@ -274,7 +278,7 @@ func Nsub(zp *Natural, x, y Natural) {
n := len(x);
m := len(y);
if n < m {
panic("underflow")
panic("underflow");
}
z := nalloc(*zp, n);
@ -329,7 +333,7 @@ func Nscale(z *Natural, d uint64) {
switch {
case d == 0:
*z = Nat(0);
return
return;
case d == 1:
return;
case d >= _B:
@ -346,7 +350,7 @@ func Nscale(z *Natural, d uint64) {
for i, d := range *z {
zz[i] = d;
}
*z = zz
*z = zz;
} else {
*z = (*z)[0 : n+1];
}
@ -376,10 +380,14 @@ func muladd1(x Natural, d, c digit) Natural {
//
func (x Natural) Mul1(d uint64) Natural {
switch {
case d == 0: return Nat(0);
case d == 1: return x;
case isSmall(digit(d)): muladd1(x, digit(d), 0);
case d >= _B: return x.Mul(Nat(d));
case d == 0:
return Nat(0);
case d == 1:
return x;
case isSmall(digit(d)):
muladd1(x, digit(d), 0);
case d >= _B:
return x.Mul(Nat(d));
}
z := make(Natural, len(x)+1);
@ -438,7 +446,9 @@ func unpack(x Natural) []digit2 {
// normalize result
k := 2*n;
for k > 0 && z[k - 1] == 0 { k-- }
for k > 0 && z[k-1] == 0 {
k--;
}
return z[0:k]; // trim leading 0's
}
@ -541,14 +551,15 @@ func divmod(x, y []digit2) ([]digit2, []digit2) {
// compute trial digit (Knuth)
var q digit;
{ x0, x1, x2 := digit(x[k]), digit(x[k-1]), digit(x[k-2]);
{
x0, x1, x2 := digit(x[k]), digit(x[k-1]), digit(x[k-2]);
if x0 != y1 {
q = (x0<<_W2 + x1)/y1;
} else {
q = _B2-1;
}
for y2*q > (x0<<_W2 + x1 - y1*q)<<_W2 + x2 {
q--
q--;
}
}
@ -565,7 +576,7 @@ func divmod(x, y []digit2) ([]digit2, []digit2) {
c := digit(0);
for j := 0; j < m; j++ {
t := c+digit(x[i+j])+digit(y[j]);
c, x[i+j] = t >> _W2, digit2(t & _M2)
c, x[i+j] = t>>_W2, digit2(t&_M2);
}
assert(c+digit(x[k]) == 0);
// correct trial digit
@ -687,7 +698,7 @@ func (x Natural) And(y Natural) Natural {
func copy(z, x Natural) {
for i, e := range x {
z[i] = e
z[i] = e;
}
}
@ -764,12 +775,16 @@ func (x Natural) Cmp(y Natural) int {
}
i := n-1;
for i > 0 && x[i] == y[i] { i--; }
for i > 0 && x[i] == y[i] {
i--;
}
d := 0;
switch {
case x[i] < y[i]: d = -1;
case x[i] > y[i]: d = 1;
case x[i] < y[i]:
d = -1;
case x[i] > y[i]:
d = 1;
}
return d;
@ -843,7 +858,7 @@ func (x Natural) ToString(base uint) string {
var d digit;
t, d = divmod1(t, digit(base));
s[i] = "0123456789abcdef"[d];
};
}
return string(s[i:n]);
}
@ -859,9 +874,12 @@ func (x Natural) String() string {
func fmtbase(c int) uint {
switch c {
case 'b': return 2;
case 'o': return 8;
case 'x': return 16;
case 'b':
return 2;
case 'o':
return 8;
case 'x':
return 16;
}
return 10;
}
@ -878,9 +896,12 @@ func (x Natural) Format(h fmt.State, c int) {
func hexvalue(ch byte) uint {
d := uint(1<<logH);
switch {
case '0' <= ch && ch <= '9': d = uint(ch - '0');
case 'a' <= ch && ch <= 'f': d = uint(ch - 'a') + 10;
case 'A' <= ch && ch <= 'F': d = uint(ch - 'A') + 10;
case '0' <= ch && ch <= '9':
d = uint(ch-'0');
case 'a' <= ch && ch <= 'f':
d = uint(ch-'a') + 10;
case 'A' <= ch && ch <= 'F':
d = uint(ch-'A') + 10;
}
return d;
}
@ -970,9 +991,12 @@ func (xp Natural) Pow(n uint) Natural {
//
func MulRange(a, b uint) Natural {
switch {
case a > b: return Nat(1);
case a == b: return Nat(uint64(a));
case a + 1 == b: return Nat(uint64(a)).Mul(Nat(uint64(b)));
case a > b:
return Nat(1);
case a == b:
return Nat(uint64(a));
case a+1 == b:
return Nat(uint64(a)).Mul(Nat(uint64(b)));
}
m := (a+b)>>1;
assert(a <= m && m < b);

View File

@ -49,18 +49,14 @@ var (
nat_zero = Nat(0);
nat_one = Nat(1);
nat_two = Nat(2);
a = natFromString(sa, 10, nil);
b = natFromString(sb, 10, nil);
c = natFromString(sc, 10, nil);
p = natFromString(sp, 10, nil);
int_zero = Int(0);
int_one = Int(1);
int_two = Int(2);
ip = intFromString(sp, 10, nil);
rat_zero = Rat(0, 1);
rat_half = Rat(1, 2);
rat_one = Rat(1, 1);
@ -68,8 +64,8 @@ var (
)
var test_msg string;
var tester *testing.T;
var test_msg string
var tester *testing.T
func test(n uint, b bool) {
if !b {
@ -102,7 +98,10 @@ func rat_eq(n uint, x, y *Rational) {
func TestNatConv(t *testing.T) {
tester = t;
test_msg = "NatConvA";
type entry1 struct { x uint64; s string };
type entry1 struct {
x uint64;
s string;
}
tab := []entry1{
entry1{0, "0"},
entry1{255, "255"},
@ -168,7 +167,10 @@ func abs(x int64) uint64 {
func TestIntConv(t *testing.T) {
tester = t;
test_msg = "IntConvA";
type entry2 struct { x int64; s string };
type entry2 struct {
x int64;
s string;
}
tab := []entry2{
entry2{0, "0"},
entry2{-128, "-128"},
@ -335,7 +337,9 @@ func TestNatDiv(t *testing.T) {
func TestIntQuoRem(t *testing.T) {
tester = t;
test_msg = "IntQuoRem";
type T struct { x, y, q, r int64 };
type T struct {
x, y, q, r int64;
}
a := []T{
T{+8, +3, +2, +2},
T{+8, -3, -2, +2},
@ -362,7 +366,9 @@ func TestIntQuoRem(t *testing.T) {
func TestIntDivMod(t *testing.T) {
tester = t;
test_msg = "IntDivMod";
type T struct { x, y, q, r int64 };
type T struct {
x, y, q, r int64;
}
a := []T{
T{+8, +3, +2, +2},
T{+8, -3, -2, +2},
@ -418,7 +424,8 @@ func TestNatShift(t *testing.T) {
}
test_msg = "NatShift3L";
{ const m = 3;
{
const m = 3;
p := b;
f := Nat(1<<m);
for i := uint(0); i < 100; i++ {
@ -428,7 +435,8 @@ func TestNatShift(t *testing.T) {
}
test_msg = "NatShift3R";
{ p := c;
{
p := c;
for i := uint(0); !p.IsZero(); i++ {
nat_eq(i, c.Shr(i), p);
p = p.Shr(1);
@ -453,7 +461,8 @@ func TestIntShift(t *testing.T) {
}
test_msg = "IntShift3L";
{ const m = 3;
{
const m = 3;
p := ip;
f := Int(1<<m);
for i := uint(0); i < 100; i++ {
@ -463,7 +472,8 @@ func TestIntShift(t *testing.T) {
}
test_msg = "IntShift3R";
{ p := ip;
{
p := ip;
for i := uint(0); p.IsPos(); i++ {
int_eq(i, ip.Shr(i), p);
p = p.Shr(1);
@ -515,7 +525,9 @@ func TestNatBitOps(t *testing.T) {
func TestIntBitOps1(t *testing.T) {
tester = t;
test_msg = "IntBitOps1";
type T struct { x, y int64 };
type T struct {
x, y int64;
}
a := []T{
T{+7, +3},
T{+7, -3},
@ -651,4 +663,3 @@ func TestNatPop(t *testing.T) {
test(i, nat_one.Shl(i).Sub(nat_one).Pop() == i);
}
}

View File

@ -95,14 +95,14 @@ func (x *Integer) IsZero() bool {
// IsNeg returns true iff x < 0.
//
func (x *Integer) IsNeg() bool {
return x.sign && !x.mant.IsZero()
return x.sign && !x.mant.IsZero();
}
// IsPos returns true iff x >= 0.
//
func (x *Integer) IsPos() bool {
return !x.sign && !x.mant.IsZero()
return !x.sign && !x.mant.IsZero();
}
@ -478,8 +478,10 @@ func (x *Integer) Cmp(y *Integer) int {
if x.sign {
r = -r;
}
case x.sign: r = -1;
case y.sign: r = 1;
case x.sign:
r = -1;
case y.sign:
r = 1;
}
return r;
}

View File

@ -55,8 +55,10 @@ func (x fpNat) mul(y fpNat) fpNat {
//
func (x fpNat) mant() Natural {
switch {
case x.e > 0: return x.m.Shl(uint(x.e));
case x.e < 0: return x.m.Shr(uint(-x.e));
case x.e > 0:
return x.m.Shl(uint(x.e));
case x.e < 0:
return x.m.Shr(uint(-x.e));
}
return x.m;
}

View File

@ -19,7 +19,7 @@ var bytes []byte // test data; same as data but as a slice.
func init() {
bytes = make([]byte, N);
for i := 0; i < N; i++ {
bytes[i] = 'a' + byte(i % 26)
bytes[i] = 'a' + byte(i%26);
}
data = string(bytes);
}
@ -29,19 +29,19 @@ func check(t *testing.T, testname string, buf *Buffer, s string) {
bytes := buf.Bytes();
str := buf.String();
if buf.Len() != len(bytes) {
t.Errorf("%s: buf.Len() == %d, len(buf.Bytes()) == %d\n", testname, buf.Len(), len(bytes))
t.Errorf("%s: buf.Len() == %d, len(buf.Bytes()) == %d\n", testname, buf.Len(), len(bytes));
}
if buf.Len() != len(str) {
t.Errorf("%s: buf.Len() == %d, len(buf.String()) == %d\n", testname, buf.Len(), len(str))
t.Errorf("%s: buf.Len() == %d, len(buf.String()) == %d\n", testname, buf.Len(), len(str));
}
if buf.Len() != len(s) {
t.Errorf("%s: buf.Len() == %d, len(s) == %d\n", testname, buf.Len(), len(s))
t.Errorf("%s: buf.Len() == %d, len(s) == %d\n", testname, buf.Len(), len(s));
}
if string(bytes) != s {
t.Errorf("%s: string(buf.Bytes()) == %q, s == %q\n", testname, string(bytes), s)
t.Errorf("%s: string(buf.Bytes()) == %q, s == %q\n", testname, string(bytes), s);
}
}

View File

@ -17,31 +17,31 @@ func Compare(a, b []byte) int {
for i := 0; i < len(a) && i < len(b); i++ {
switch {
case a[i] > b[i]:
return 1
return 1;
case a[i] < b[i]:
return -1
return -1;
}
}
switch {
case len(a) < len(b):
return -1
return -1;
case len(a) > len(b):
return 1
return 1;
}
return 0
return 0;
}
// Equal returns a boolean reporting whether a == b.
func Equal(a, b []byte) bool {
if len(a) != len(b) {
return false
return false;
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
return false;
}
}
return true
return true;
}
// Copy copies bytes from src to dst,
@ -53,9 +53,9 @@ func Copy(dst, src []byte) int {
src = src[0:len(dst)];
}
for i, x := range src {
dst[i] = x
dst[i] = x;
}
return len(src)
return len(src);
}
// explode splits s into an array of UTF-8 sequences, one per Unicode character (still arrays of bytes),
@ -71,45 +71,45 @@ func explode(s []byte, n int) [][]byte {
if na+1 >= n {
a[na] = s;
na++;
break
break;
}
_, size = utf8.DecodeRune(s);
a[na] = s[0:size];
s = s[size:len(s)];
na++;
}
return a[0:na]
return a[0:na];
}
// Count counts the number of non-overlapping instances of sep in s.
func Count(s, sep []byte) int {
if len(sep) == 0 {
return utf8.RuneCount(s)+1
return utf8.RuneCount(s) + 1;
}
c := sep[0];
n := 0;
for i := 0; i+len(sep) <= len(s); i++ {
if s[i] == c && (len(sep) == 1 || Equal(s[i : i+len(sep)], sep)) {
n++;
i += len(sep)-1
i += len(sep)-1;
}
}
return n
return n;
}
// Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
func Index(s, sep []byte) int {
n := len(sep);
if n == 0 {
return 0
return 0;
}
c := sep[0];
for i := 0; i+n <= len(s); i++ {
if s[i] == c && (n == 1 || Equal(s[i : i+n], sep)) {
return i
return i;
}
}
return -1
return -1;
}
// Split splits the array s around each instance of sep, returning an array of subarrays of s.
@ -117,7 +117,7 @@ func Index(s, sep []byte) int {
// If n > 0, split Splits s into at most n subarrays; the last subarray will contain an unsplit remainder.
func Split(s, sep []byte, n int) [][]byte {
if len(sep) == 0 {
return explode(s, n)
return explode(s, n);
}
if n <= 0 {
n = Count(s, sep) + 1;
@ -135,21 +135,21 @@ func Split(s, sep []byte, n int) [][]byte {
}
}
a[na] = s[start:len(s)];
return a[0:na+1]
return a[0 : na+1];
}
// Join concatenates the elements of a to create a single byte array. The separator
// sep is placed between elements in the resulting array.
func Join(a [][]byte, sep []byte) []byte {
if len(a) == 0 {
return []byte{}
return []byte{};
}
if len(a) == 1 {
return a[0]
return a[0];
}
n := len(sep)*(len(a)-1);
for i := 0; i < len(a); i++ {
n += len(a[i])
n += len(a[i]);
}
b := make([]byte, n);
@ -158,27 +158,27 @@ func Join(a [][]byte, sep []byte) []byte {
s := a[i];
for j := 0; j < len(s); j++ {
b[bp] = s[j];
bp++
bp++;
}
if i+1 < len(a) {
s = sep;
for j := 0; j < len(s); j++ {
b[bp] = s[j];
bp++
bp++;
}
}
}
return b
return b;
}
// HasPrefix tests whether the byte array s begins with prefix.
func HasPrefix(s, prefix []byte) bool {
return len(s) >= len(prefix) && Equal(s[0:len(prefix)], prefix)
return len(s) >= len(prefix) && Equal(s[0:len(prefix)], prefix);
}
// HasSuffix tests whether the byte array s ends with suffix.
func HasSuffix(s, suffix []byte) bool {
return len(s) >= len(suffix) && Equal(s[len(s)-len(suffix):len(s)], suffix)
return len(s) >= len(suffix) && Equal(s[len(s)-len(suffix) : len(s)], suffix);
}
// Map returns a copy of the byte array s with all its characters modified
@ -204,7 +204,7 @@ func Map(mapping func(rune int) int, s []byte) []byte {
maxbytes = maxbytes*2 + utf8.UTFMax;
nb := make([]byte, maxbytes);
for i, c := range b[0:nbytes] {
nb[i] = c
nb[i] = c;
}
b = nb;
}
@ -216,17 +216,17 @@ func Map(mapping func(rune int) int, s []byte) []byte {
// ToUpper returns a copy of the byte array s with all Unicode letters mapped to their upper case.
func ToUpper(s []byte) []byte {
return Map(unicode.ToUpper, s)
return Map(unicode.ToUpper, s);
}
// ToUpper returns a copy of the byte array s with all Unicode letters mapped to their lower case.
func ToLower(s []byte) []byte {
return Map(unicode.ToLower, s)
return Map(unicode.ToLower, s);
}
// ToTitle returns a copy of the byte array s with all Unicode letters mapped to their title case.
func Title(s []byte) []byte {
return Map(unicode.ToTitle, s)
return Map(unicode.ToTitle, s);
}
// Trim returns a slice of the string s, with all leading and trailing white space
@ -237,7 +237,7 @@ func TrimSpace(s []byte) []byte {
wid := 1;
rune := int(s[start]);
if rune >= utf8.RuneSelf {
rune, wid = utf8.DecodeRune(s[start:end])
rune, wid = utf8.DecodeRune(s[start:end]);
}
if !unicode.IsSpace(rune) {
break;
@ -249,10 +249,9 @@ func TrimSpace(s []byte) []byte {
rune := int(s[end-1]);
if rune >= utf8.RuneSelf {
// Back up carefully looking for beginning of rune. Mustn't pass start.
for wid = 2; start <= end-wid && !utf8.RuneStart(s[end-wid]); wid++ {
}
for wid = 2; start <= end-wid && !utf8.RuneStart(s[end-wid]); wid++ {}
if start > end-wid { // invalid UTF-8 sequence; stop processing
return s[start:end]
return s[start:end];
}
rune, wid = utf8.DecodeRune(s[end-wid : end]);
}
@ -268,7 +267,7 @@ func TrimSpace(s []byte) []byte {
// Heuristic: Scale by 50% to give n log n time.
func resize(n int) int {
if n < 16 {
n = 16
n = 16;
}
return n + n/2;
}

View File

@ -72,8 +72,8 @@ var initDecoderTests = []*InitDecoderTest{
[]int{2, 1, 3, 3},
huffmanDecoder{
1, 3,
[maxCodeLen+1]int{ 1: 0, 2, 7, },
[maxCodeLen+1]int{ 1: 0, 1, 4, },
[maxCodeLen+1]int{1: 0, 2, 7},
[maxCodeLen+1]int{1: 0, 1, 4},
[]int{1, 0, 2, 3},
},
true,
@ -84,8 +84,8 @@ var initDecoderTests = []*InitDecoderTest{
[]int{3, 3, 3, 3, 3, 2, 4, 4},
huffmanDecoder{
2, 4,
[maxCodeLen+1]int{ 2: 0, 6, 15, },
[maxCodeLen+1]int{ 2: 0, 1, 8, },
[maxCodeLen+1]int{2: 0, 6, 15},
[maxCodeLen+1]int{2: 0, 1, 8},
[]int{5, 0, 1, 2, 3, 4, 6, 7},
},
true,
@ -107,7 +107,7 @@ var initDecoderTests = []*InitDecoderTest{
// Illegal input.
&InitDecoderTest{
[]int{ 0, 0, 0, 0, 0, 0, 0, },
[]int{0, 0, 0, 0, 0, 0, 0},
huffmanDecoder{},
false,
},
@ -127,8 +127,7 @@ func TestInitDecoder(t *testing.T) {
}
func TestUncompressedSource(t *testing.T) {
decoder := NewInflater(bytes.NewBuffer(
[]byte{ 0x01, 0x01, 0x00, 0xfe, 0xff, 0x11 }));
decoder := NewInflater(bytes.NewBuffer([]byte{0x01, 0x01, 0x00, 0xfe, 0xff, 0x11}));
output := make([]byte, 1);
n, error := decoder.Read(output);
if n != 1 || error != nil {

View File

@ -24,12 +24,14 @@ const (
// A CorruptInputError reports the presence of corrupt input at a given offset.
type CorruptInputError int64
func (e CorruptInputError) String() string {
return "flate: corrupt input before offset " + strconv.Itoa64(int64(e));
}
// An InternalError reports an error in the flate code itself.
type InternalError string
func (e InternalError) String() string {
return "flate: internal error: " + string(e);
}
@ -41,8 +43,7 @@ type ReadError struct {
}
func (e *ReadError) String() string {
return "flate: read error at offset " + strconv.Itoa64(e.Offset)
+ ": " + e.Error.String();
return "flate: read error at offset " + strconv.Itoa64(e.Offset) + ": " + e.Error.String();
}
// A WriteError reports an error encountered while writing output.
@ -52,8 +53,7 @@ type WriteError struct {
}
func (e *WriteError) String() string {
return "flate: write error at offset " + strconv.Itoa64(e.Offset)
+ ": " + e.Error.String();
return "flate: write error at offset " + strconv.Itoa64(e.Offset) + ": " + e.Error.String();
}
// Huffman decoder is based on
@ -142,8 +142,8 @@ func (h *huffmanDecoder) init(bits []int) bool {
// See RFC 1951, section 3.2.6.
var fixedHuffmanDecoder = huffmanDecoder{
7, 9,
[maxCodeLen+1]int{ 7: 23, 199, 511, },
[maxCodeLen+1]int{ 7: 0, 24, 224, },
[maxCodeLen+1]int{7: 23, 199, 511},
[maxCodeLen+1]int{7: 0, 24, 224},
[]int{
// length 7: 256-279
256, 257, 258, 259, 260, 261, 262,
@ -187,7 +187,7 @@ var fixedHuffmanDecoder = huffmanDecoder{
232, 233, 234, 235, 236, 237, 238, 239,
240, 241, 242, 243, 244, 245, 246, 247,
248, 249, 250, 251, 252, 253, 254, 255,
}
},
}
// The actual read interface needed by NewInflater.
@ -262,9 +262,7 @@ func (f *inflater) inflate() (err os.Error) {
// RFC 1951 section 3.2.7.
// Compression with dynamic Huffman codes
var codeOrder = [...]int {
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
}
var codeOrder = [...]int{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}
func (f *inflater) readHuffman() os.Error {
// HLIT[5], HDIST[5], HCLEN[4].

View File

@ -46,4 +46,3 @@ func reverseUint16(v uint16) uint16 {
func reverseBits(number uint16, bitLength byte) uint16 {
return reverseUint16(number << uint8(16-bitLength));
}

View File

@ -43,7 +43,7 @@ var lengthCodes = [...]uint32 {
26, 26, 26, 26, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 28
27, 27, 27, 27, 27, 28,
}
var offsetCodes = [...]uint32{
@ -65,7 +65,7 @@ var offsetCodes = [...]uint32 {
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
}
type token uint32;
type token uint32
// Convert a literal into a literal token.
func literalToken(literal uint32) token {
@ -113,4 +113,3 @@ func offsetCode(off uint32) uint32 {
}
panic("unreachable");
}

View File

@ -70,4 +70,3 @@ func copyUint8s(dst []uint8, src []uint8) int {
}
return cnt;
}

View File

@ -18,9 +18,7 @@ import (
const (
gzipID1 = 0x1f;
gzipID2 = 0x8b;
gzipDeflate = 8;
flagText = 1<<0;
flagHdrCrc = 1<<1;
flagExtra = 1<<2;
@ -225,4 +223,3 @@ func (z *Inflater) Read(p []byte) (n int, err os.Error) {
func (z *Inflater) Close() os.Error {
return z.inflater.Close();
}

View File

@ -93,4 +93,3 @@ func (z *reader) Close() os.Error {
z.err = z.inflater.Close();
return z.err;
}

View File

@ -25,10 +25,8 @@ var zlibTests = []zlibTest {
zlibTest{
"empty",
"",
[]byte {
0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01,
},
nil
[]byte{0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01},
nil,
},
zlibTest{
"goodbye",
@ -38,30 +36,24 @@ var zlibTests = []zlibTest {
0x4c, 0xd5, 0x51, 0x28, 0xcf, 0x2f, 0xca, 0x49,
0x01, 0x00, 0x28, 0xa5, 0x05, 0x5e,
},
nil
nil,
},
zlibTest{
"bad header",
"",
[]byte {
0x78, 0x9f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01,
},
HeaderError
[]byte{0x78, 0x9f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01},
HeaderError,
},
zlibTest{
"bad checksum",
"",
[]byte {
0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0xff,
},
[]byte{0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0xff},
ChecksumError,
},
zlibTest{
"not enough data",
"",
[]byte {
0x78, 0x9c, 0x03, 0x00, 0x00, 0x00,
},
[]byte{0x78, 0x9c, 0x03, 0x00, 0x00, 0x00},
io.ErrUnexpectedEOF,
},
zlibTest{

View File

@ -104,4 +104,3 @@ func (z *writer) Close() os.Error {
_, z.err = z.w.Write(z.scratch[0:4]);
return z.err;
}

View File

@ -20,12 +20,12 @@ type Element struct {
// Next returns the next list element or nil.
func (e *Element) Next() *Element {
return e.next
return e.next;
}
// Prev returns the previous list element or nil.
func (e *Element) Prev() *Element {
return e.prev
return e.prev;
}
// List represents a doubly linked list.
@ -41,28 +41,28 @@ func (l *List) Init() *List {
l.back = nil;
l.len = 0;
l.id = new(byte);
return l
return l;
}
// New returns an initialized list.
func New() *List {
return new(List).Init()
return new(List).Init();
}
// Front returns the first element in the list.
func (l *List) Front() *Element {
return l.front
return l.front;
}
// Back returns the last element in the list.
func (l *List) Back() *Element {
return l.back
return l.back;
}
// Remove removes the element from the list.
func (l *List) Remove(e *Element) {
if e.id != l.id {
return
return;
}
if e.prev == nil {
l.front = e.next;
@ -112,7 +112,7 @@ func (l *List) insertFront(e *Element) {
l.front, l.back = e, e;
e.prev, e.next = nil, nil;
l.len = 1;
return
return;
}
l.insertBefore(e, l.front);
}
@ -123,7 +123,7 @@ func (l *List) insertBack(e *Element) {
l.front, l.back = e, e;
e.prev, e.next = nil, nil;
l.len = 1;
return
return;
}
l.insertAfter(e, l.back);
}
@ -135,7 +135,7 @@ func (l *List) PushFront(value interface {}) *Element {
}
e := &Element{nil, nil, l.id, value};
l.insertFront(e);
return e
return e;
}
// PushBack inserts the value at the back of the list and returns a new Element containing the value.
@ -145,33 +145,33 @@ func (l *List) PushBack(value interface {}) *Element {
}
e := &Element{nil, nil, l.id, value};
l.insertBack(e);
return e
return e;
}
// InsertBefore inserts the value immediately before mark and returns a new Element containing the value.
func (l *List) InsertBefore(value interface{}, mark *Element) *Element {
if mark.id != l.id {
return nil
return nil;
}
e := &Element{nil, nil, l.id, value};
l.insertBefore(e, mark);
return e
return e;
}
// InsertAfter inserts the value immediately after mark and returns a new Element containing the value.
func (l *List) InsertAfter(value interface{}, mark *Element) *Element {
if mark.id != l.id {
return nil
return nil;
}
e := &Element{nil, nil, l.id, value};
l.insertAfter(e, mark);
return e
return e;
}
// MoveToFront moves the element to the front of the list.
func (l *List) MoveToFront(e *Element) {
if e.id != l.id || l.front == e {
return
return;
}
l.Remove(e);
l.insertFront(e);
@ -180,7 +180,7 @@ func (l *List) MoveToFront(e *Element) {
// MoveToBack moves the element to the back of the list.
func (l *List) MoveToBack(e *Element) {
if e.id != l.id || l.back == e {
return
return;
}
l.Remove(e);
l.insertBack(e);
@ -188,7 +188,7 @@ func (l *List) MoveToBack(e *Element) {
// Len returns the number of elements in the list.
func (l *List) Len() int {
return l.len
return l.len;
}
func (l *List) iterate(c chan<- interface{}) {
@ -201,5 +201,5 @@ func (l *List) iterate(c chan<- interface {}) {
func (l *List) Iter() <-chan interface{} {
c := make(chan interface{});
go l.iterate(c);
return c
return c;
}

View File

@ -13,7 +13,7 @@ func checkListPointers(t *testing.T, l *List, es []*Element) {
if l.front != nil || l.back != nil {
t.Errorf("l.front/l.back = %v/%v should be nil/nil", l.front, l.back);
}
return
return;
}
if l.front != es[0] {

View File

@ -149,5 +149,5 @@ func (r *Ring) Iter() <-chan interface {} {
}
close(c);
}();
return c
return c;
}

View File

@ -22,25 +22,25 @@ func (p *IntVector) Init(len int) *IntVector {
// NewIntVector returns an initialized new IntVector with length at least len.
func NewIntVector(len int) *IntVector {
return new(IntVector).Init(len)
return new(IntVector).Init(len);
}
// At returns the i'th element of the vector.
func (p *IntVector) At(i int) int {
return p.Vector.At(i).(int)
return p.Vector.At(i).(int);
}
// Set sets the i'th element of the vector to value x.
func (p *IntVector) Set(i int, x int) {
p.a[i] = x
p.a[i] = x;
}
// Last returns the element in the vector of highest index.
func (p *IntVector) Last() int {
return p.Vector.Last().(int)
return p.Vector.Last().(int);
}
@ -48,23 +48,23 @@ func (p *IntVector) Last() int {
func (p *IntVector) Data() []int {
arr := make([]int, p.Len());
for i, v := range p.a {
arr[i] = v.(int)
arr[i] = v.(int);
}
return arr
return arr;
}
// Insert inserts into the vector an element of value x before
// the current element at index i.
func (p *IntVector) Insert(i int, x int) {
p.Vector.Insert(i, x)
p.Vector.Insert(i, x);
}
// InsertVector inserts into the vector the contents of the Vector
// x such that the 0th element of x appears at index i after insertion.
func (p *IntVector) InsertVector(i int, x *IntVector) {
p.Vector.InsertVector(i, &x.Vector)
p.Vector.InsertVector(i, &x.Vector);
}
@ -77,13 +77,13 @@ func (p *IntVector) Slice(i, j int) *IntVector {
// Push appends x to the end of the vector.
func (p *IntVector) Push(x int) {
p.Vector.Push(x)
p.Vector.Push(x);
}
// Pop deletes and returns the last element of the vector.
func (p *IntVector) Pop() int {
return p.Vector.Pop().(int)
return p.Vector.Pop().(int);
}
@ -96,14 +96,14 @@ func (p *IntVector) AppendVector(x *IntVector) {
// SortInterface support
// Less returns a boolean denoting whether the i'th element is less than the j'th element.
func (p *IntVector) Less(i, j int) bool {
return p.At(i) < p.At(j)
return p.At(i) < p.At(j);
}
// Iterate over all elements; driver for range
func (p *IntVector) iterate(c chan<- int) {
for _, v := range p.a {
c <- v.(int)
c <- v.(int);
}
close(c);
}

View File

@ -21,25 +21,25 @@ func (p *StringVector) Init(len int) *StringVector {
// NewStringVector returns an initialized new StringVector with length at least len.
func NewStringVector(len int) *StringVector {
return new(StringVector).Init(len)
return new(StringVector).Init(len);
}
// At returns the i'th element of the vector.
func (p *StringVector) At(i int) string {
return p.Vector.At(i).(string)
return p.Vector.At(i).(string);
}
// Set sets the i'th element of the vector to value x.
func (p *StringVector) Set(i int, x string) {
p.a[i] = x
p.a[i] = x;
}
// Last returns the element in the vector of highest index.
func (p *StringVector) Last() string {
return p.Vector.Last().(string)
return p.Vector.Last().(string);
}
@ -47,23 +47,23 @@ func (p *StringVector) Last() string {
func (p *StringVector) Data() []string {
arr := make([]string, p.Len());
for i, v := range p.a {
arr[i] = v.(string)
arr[i] = v.(string);
}
return arr
return arr;
}
// Insert inserts into the vector an element of value x before
// the current element at index i.
func (p *StringVector) Insert(i int, x string) {
p.Vector.Insert(i, x)
p.Vector.Insert(i, x);
}
// InsertVector inserts into the vector the contents of the Vector
// x such that the 0th element of x appears at index i after insertion.
func (p *StringVector) InsertVector(i int, x *StringVector) {
p.Vector.InsertVector(i, &x.Vector)
p.Vector.InsertVector(i, &x.Vector);
}
@ -76,13 +76,13 @@ func (p *StringVector) Slice(i, j int) *StringVector {
// Push appends x to the end of the vector.
func (p *StringVector) Push(x string) {
p.Vector.Push(x)
p.Vector.Push(x);
}
// Pop deletes and returns the last element of the vector.
func (p *StringVector) Pop() string {
return p.Vector.Pop().(string)
return p.Vector.Pop().(string);
}
@ -95,14 +95,14 @@ func (p *StringVector) AppendVector(x *StringVector) {
// SortInterface support
// Less returns a boolean denoting whether the i'th element is less than the j'th element.
func (p *StringVector) Less(i, j int) bool {
return p.At(i) < p.At(j)
return p.At(i) < p.At(j);
}
// Iterate over all elements; driver for range
func (p *StringVector) iterate(c chan<- string) {
for _, v := range p.a {
c <- v.(string)
c <- v.(string);
}
close(c);
}

View File

@ -14,13 +14,13 @@ type Element interface {}
// Vector is the container itself.
// The zero value for Vector is an empty vector ready to use.
type Vector struct {
a []Element
a []Element;
}
func copy(dst, src []Element) {
for i := 0; i < len(src); i++ {
dst[i] = src[i]
dst[i] = src[i];
}
}
@ -32,25 +32,25 @@ func expand(a []Element, i, n int) []Element {
len1 := len0+n;
if len1 < cap(a) {
// enough space - just expand
a = a[0 : len1]
a = a[0:len1];
} else {
// not enough space - double capacity
capb := cap(a)*2;
if capb < len1 {
// still not enough - use required length
capb = len1
capb = len1;
}
// capb >= len1
b := make([]Element, len1, capb);
copy(b, a);
a = b
a = b;
}
// make a hole
for j := len0-1; j >= i; j-- {
a[j+n] = a[j]
a[j+n] = a[j];
}
return a
return a;
}
@ -63,24 +63,24 @@ func (p *Vector) Init(initial_len int) *Vector {
if cap(a) == 0 || cap(a) < initial_len {
n := 8; // initial capacity
if initial_len > n {
n = initial_len
n = initial_len;
}
a = make([]Element, n);
} else {
// nil out entries
for j := len(a)-1; j >= 0; j-- {
a[j] = nil
a[j] = nil;
}
}
p.a = a[0:initial_len];
return p
return p;
}
// New returns an initialized new Vector with length at least len.
func New(len int) *Vector {
return new(Vector).Init(len)
return new(Vector).Init(len);
}
@ -90,25 +90,25 @@ func (p *Vector) Len() int {
if p == nil {
return 0;
}
return len(p.a)
return len(p.a);
}
// At returns the i'th element of the vector.
func (p *Vector) At(i int) Element {
return p.a[i]
return p.a[i];
}
// Set sets the i'th element of the vector to value x.
func (p *Vector) Set(i int, x Element) {
p.a[i] = x
p.a[i] = x;
}
// Last returns the element in the vector of highest index.
func (p *Vector) Last() Element {
return p.a[len(p.a) - 1]
return p.a[len(p.a) - 1];
}
@ -116,9 +116,9 @@ func (p *Vector) Last() Element {
func (p *Vector) Data() []Element {
arr := make([]Element, p.Len());
for i, v := range p.a {
arr[i] = v
arr[i] = v;
}
return arr
return arr;
}
@ -158,7 +158,7 @@ func (p *Vector) Cut(i, j int) {
copy(a[i:m], a[j:n]);
for k := m; k < n; k++ {
a[k] = nil // support GC, nil out entries
a[k] = nil; // support GC, nil out entries
}
p.a = a[0:m];
@ -178,7 +178,7 @@ func (p *Vector) Slice(i, j int) *Vector {
// The function should not change the indexing of the vector underfoot.
func (p *Vector) Do(f func(elem Element)) {
for i := 0; i < len(p.a); i++ {
f(p.a[i]) // not too safe if f changes the Vector
f(p.a[i]); // not too safe if f changes the Vector
}
}
@ -187,7 +187,7 @@ func (p *Vector) Do(f func(elem Element)) {
// Push appends x to the end of the vector.
func (p *Vector) Push(x Element) {
p.Insert(len(p.a), x)
p.Insert(len(p.a), x);
}
@ -211,27 +211,27 @@ func (p *Vector) AppendVector(x *Vector) {
// LessInterface provides partial support of the SortInterface.
type LessInterface interface {
Less(y Element) bool
Less(y Element) bool;
}
// Less returns a boolean denoting whether the i'th element is less than the j'th element.
func (p *Vector) Less(i, j int) bool {
return p.a[i].(LessInterface).Less(p.a[j])
return p.a[i].(LessInterface).Less(p.a[j]);
}
// Swap exchanges the elements at indexes i and j.
func (p *Vector) Swap(i, j int) {
a := p.a;
a[i], a[j] = a[j], a[i]
a[i], a[j] = a[j], a[i];
}
// Iterate over all elements; driver for range
func (p *Vector) iterate(c chan<- Element) {
for _, v := range p.a {
c <- v
c <- v;
}
close(c);
}

View File

@ -11,29 +11,45 @@ import "fmt"
func TestZeroLen(t *testing.T) {
var a *Vector;
if a.Len() != 0 { t.Errorf("A) expected 0, got %d", a.Len()); }
if a.Len() != 0 {
t.Errorf("A) expected 0, got %d", a.Len());
}
a = New(0);
if a.Len() != 0 { t.Errorf("B) expected 0, got %d", a.Len()); }
if a.Len() != 0 {
t.Errorf("B) expected 0, got %d", a.Len());
}
}
func TestInit(t *testing.T) {
var a Vector;
if a.Init(0).Len() != 0 { t.Error("A") }
if a.Init(1).Len() != 1 { t.Error("B") }
if a.Init(10).Len() != 10 { t.Error("C") }
if a.Init(0).Len() != 0 {
t.Error("A");
}
if a.Init(1).Len() != 1 {
t.Error("B");
}
if a.Init(10).Len() != 10 {
t.Error("C");
}
}
func TestNew(t *testing.T) {
if New(0).Len() != 0 { t.Error("A") }
if New(1).Len() != 1 { t.Error("B") }
if New(10).Len() != 10 { t.Error("C") }
if New(0).Len() != 0 {
t.Error("A");
}
if New(1).Len() != 1 {
t.Error("B");
}
if New(10).Len() != 10 {
t.Error("C");
}
}
func val(i int) int {
return i*991 - 1234
return i*991 - 1234;
}
@ -45,7 +61,9 @@ func TestAccess(t *testing.T) {
a.Set(i, val(i));
}
for i := 0; i < n; i++ {
if a.At(i).(int) != val(i) { t.Error(i) }
if a.At(i).(int) != val(i) {
t.Error(i);
}
}
}
@ -55,25 +73,43 @@ func TestInsertDeleteClear(t *testing.T) {
var a Vector;
for i := 0; i < n; i++ {
if a.Len() != i { t.Errorf("A) wrong len %d (expected %d)", a.Len(), i) }
if a.Len() != i {
t.Errorf("A) wrong len %d (expected %d)", a.Len(), i);
}
a.Insert(0, val(i));
if a.Last().(int) != val(0) { t.Error("B") }
if a.Last().(int) != val(0) {
t.Error("B");
}
}
for i := n-1; i >= 0; i-- {
if a.Last().(int) != val(0) { t.Error("C") }
if a.At(0).(int) != val(i) { t.Error("D") }
if a.Last().(int) != val(0) {
t.Error("C");
}
if a.At(0).(int) != val(i) {
t.Error("D");
}
a.Delete(0);
if a.Len() != i { t.Errorf("E) wrong len %d (expected %d)", a.Len(), i) }
if a.Len() != i {
t.Errorf("E) wrong len %d (expected %d)", a.Len(), i);
}
}
if a.Len() != 0 { t.Errorf("F) wrong len %d (expected 0)", a.Len()) }
if a.Len() != 0 {
t.Errorf("F) wrong len %d (expected 0)", a.Len());
}
for i := 0; i < n; i++ {
a.Push(val(i));
if a.Len() != i+1 { t.Errorf("G) wrong len %d (expected %d)", a.Len(), i+1) }
if a.Last().(int) != val(i) { t.Error("H") }
if a.Len() != i+1 {
t.Errorf("G) wrong len %d (expected %d)", a.Len(), i+1);
}
if a.Last().(int) != val(i) {
t.Error("H");
}
}
a.Init(0);
if a.Len() != 0 { t.Errorf("I wrong len %d (expected 0)", a.Len()) }
if a.Len() != 0 {
t.Errorf("I wrong len %d (expected 0)", a.Len());
}
const m = 5;
for j := 0; j < m; j++ {
@ -81,25 +117,31 @@ func TestInsertDeleteClear(t *testing.T) {
for i := 0; i < n; i++ {
x := val(i);
a.Push(x);
if a.Pop().(int) != x { t.Error("J") }
if a.Len() != j+1 { t.Errorf("K) wrong len %d (expected %d)", a.Len(), j+1) }
if a.Pop().(int) != x {
t.Error("J");
}
if a.Len() != j+1 {
t.Errorf("K) wrong len %d (expected %d)", a.Len(), j+1);
}
}
if a.Len() != m { t.Errorf("L) wrong len %d (expected %d)", a.Len(), m) }
}
if a.Len() != m {
t.Errorf("L) wrong len %d (expected %d)", a.Len(), m);
}
}
func verify_slice(t *testing.T, x *Vector, elt, i, j int) {
for k := i; k < j; k++ {
if x.At(k).(int) != elt {
t.Errorf("M) wrong [%d] element %d (expected %d)", k, x.At(k).(int), elt)
t.Errorf("M) wrong [%d] element %d (expected %d)", k, x.At(k).(int), elt);
}
}
s := x.Slice(i, j);
for k, n := 0, j-i; k < n; k++ {
if s.At(k).(int) != elt {
t.Errorf("N) wrong [%d] element %d (expected %d)", k, x.At(k).(int), elt)
t.Errorf("N) wrong [%d] element %d (expected %d)", k, x.At(k).(int), elt);
}
}
}
@ -108,7 +150,7 @@ func verify_slice(t *testing.T, x *Vector, elt, i, j int) {
func verify_pattern(t *testing.T, x *Vector, a, b, c int) {
n := a+b+c;
if x.Len() != n {
t.Errorf("O) wrong len %d (expected %d)", x.Len(), n)
t.Errorf("O) wrong len %d (expected %d)", x.Len(), n);
}
verify_slice(t, x, 0, 0, a);
verify_slice(t, x, 1, a, a+b);
@ -157,13 +199,17 @@ func TestSorting(t *testing.T) {
for i := n-1; i >= 0; i-- {
a.Set(i, n-1-i);
}
if sort.IsSorted(a) { t.Error("int vector not sorted") }
if sort.IsSorted(a) {
t.Error("int vector not sorted");
}
b := NewStringVector(n);
for i := n-1; i >= 0; i-- {
b.Set(i, fmt.Sprint(n-1-i));
}
if sort.IsSorted(b) { t.Error("string vector not sorted") }
if sort.IsSorted(b) {
t.Error("string vector not sorted");
}
}
@ -175,17 +221,15 @@ func TestDo(t *testing.T) {
a.Set(i, salt*i);
}
count := 0;
a.Do(
func(e Element) {
a.Do(func(e Element) {
i := e.(int);
if i != count*salt {
t.Error("value at", count, "should be", count*salt, "not", i)
t.Error("value at", count, "should be", count*salt, "not", i);
}
count++;
}
);
});
if count != n {
t.Error("should visit", n, "values; did visit", count)
t.Error("should visit", n, "values; did visit", count);
}
}
@ -199,11 +243,11 @@ func TestIter(t *testing.T) {
i := 0;
for v := range x.Iter() {
if v.(int) != i*i {
t.Error("Iter expected", i*i, "got", v.(int))
t.Error("Iter expected", i*i, "got", v.(int));
}
i++;
}
if i != Len {
t.Error("Iter stopped at", i, "not", Len)
t.Error("Iter stopped at", i, "not", Len);
}
}