Browse Source

Add tests for ReadBytesMaxInto(..., nil)

Jakob Borg 11 years ago
parent
commit
c2f0c2225a
1 changed files with 25 additions and 1 deletions
  1. 25 1
      xdr/xdr_test.go

+ 25 - 1
xdr/xdr_test.go

@@ -56,7 +56,7 @@ func TestBytesGiven(t *testing.T) {
 	}
 }
 
-func TestReadMaxInto(t *testing.T) {
+func TestReadBytesMaxInto(t *testing.T) {
 	var max = 64
 	for tot := 32; tot < 128; tot++ {
 		for diff := -32; diff <= 32; diff++ {
@@ -80,3 +80,27 @@ func TestReadMaxInto(t *testing.T) {
 		}
 	}
 }
+
+func TestReadBytesMaxIntoNil(t *testing.T) {
+	for tot := 42; tot < 72; tot++ {
+		for max := 0; max < 128; max++ {
+			var b = new(bytes.Buffer)
+			var r = NewReader(b)
+			var w = NewWriter(b)
+
+			var toWrite = make([]byte, tot)
+			w.WriteBytes(toWrite)
+
+			var bs = r.ReadBytesMaxInto(max, nil)
+			var read = len(bs)
+
+			if max == 0 || tot <= max {
+				if read != tot {
+					t.Errorf("Incorrect read bytes, wrote=%d, max=%d, read=%d", tot, max, read)
+				}
+			} else if r.err != ErrElementSizeExceeded {
+				t.Errorf("Unexpected non-ErrElementSizeExceeded error for wrote=%d, max=%d, read=%d: %v", tot, max, read, r.err)
+			}
+		}
+	}
+}