/// <exception cref="CharacterCodingException"/> public virtual void TestTextText() { Org.Apache.Hadoop.IO.Text a = new Org.Apache.Hadoop.IO.Text("abc"); Org.Apache.Hadoop.IO.Text b = new Org.Apache.Hadoop.IO.Text("a"); b.Set(a); Assert.Equal("abc", b.ToString()); a.Append(Runtime.GetBytesForString("xdefgxxx"), 1, 4); Assert.Equal("modified aliased string", "abc", b.ToString()); Assert.Equal("appended string incorrectly", "abcdefg", a.ToString ()); // add an extra byte so that capacity = 14 and length = 8 a.Append(new byte[] { (byte)('d') }, 0, 1); Assert.Equal(14, a.GetBytes().Length); Assert.Equal(8, a.CopyBytes().Length); }
/// <exception cref="System.Exception"/> public virtual void TestCoding() { string before = "Bad \t encoding \t testcase"; Org.Apache.Hadoop.IO.Text text = new Org.Apache.Hadoop.IO.Text(before); string after = text.ToString(); Assert.True(before.Equals(after)); for (int i = 0; i < NumIterations; i++) { // generate a random string if (i == 0) { before = GetLongString(); } else { before = GetTestString(); } // test string to utf8 ByteBuffer bb = Org.Apache.Hadoop.IO.Text.Encode(before); byte[] utf8Text = ((byte[])bb.Array()); byte[] utf8Java = Runtime.GetBytesForString(before, "UTF-8"); Assert.Equal(0, WritableComparator.CompareBytes(utf8Text, 0, bb .Limit(), utf8Java, 0, utf8Java.Length)); // test utf8 to string after = Org.Apache.Hadoop.IO.Text.Decode(utf8Java); Assert.True(before.Equals(after)); } }
/// <exception cref="System.IO.IOException"/> public virtual void TestReadWithKnownLength() { string line = "hello world"; byte[] inputBytes = Runtime.GetBytesForString(line, Charsets.Utf8); DataInputBuffer @in = new DataInputBuffer(); Org.Apache.Hadoop.IO.Text text = new Org.Apache.Hadoop.IO.Text(); @in.Reset(inputBytes, inputBytes.Length); text.ReadWithKnownLength(@in, 5); Assert.Equal("hello", text.ToString()); // Read longer length, make sure it lengthens @in.Reset(inputBytes, inputBytes.Length); text.ReadWithKnownLength(@in, 7); Assert.Equal("hello w", text.ToString()); // Read shorter length, make sure it shortens @in.Reset(inputBytes, inputBytes.Length); text.ReadWithKnownLength(@in, 2); Assert.Equal("he", text.ToString()); }
/// <exception cref="System.Exception"/> public virtual void TestClear() { // Test lengths on an empty text object Org.Apache.Hadoop.IO.Text text = new Org.Apache.Hadoop.IO.Text(); Assert.Equal("Actual string on an empty text object must be an empty string" , string.Empty, text.ToString()); Assert.Equal("Underlying byte array length must be zero", 0, text .GetBytes().Length); Assert.Equal("String's length must be zero", 0, text.GetLength ()); // Test if clear works as intended text = new Org.Apache.Hadoop.IO.Text("abcd\u20acbdcd\u20ac"); int len = text.GetLength(); text.Clear(); Assert.Equal("String must be empty after clear()", string.Empty , text.ToString()); Assert.True("Length of the byte array must not decrease after clear()" , text.GetBytes().Length >= len); Assert.Equal("Length of the string must be reset to 0 after clear()" , 0, text.GetLength()); }