use of com.google.protobuf.ByteString in project j2objc by google.
the class CompatibilityTest method testByteStringSubstring.
public void testByteStringSubstring() throws Exception {
ByteString bs1 = ByteString.copyFrom("abcdefghijklmnop".getBytes());
ByteString bs2 = bs1.substring(1, 15);
assertEquals("bcdefghijklmno", new String(bs2.toByteArray()));
ByteString bs3 = bs1.substring(12);
assertEquals("mnop", new String(bs3.toByteArray()));
ByteString bs4 = bs1.substring(11, 11);
assertTrue(bs4.isEmpty());
expectSubstringIndexOutOfBounds(bs1, -1, 1);
expectSubstringIndexOutOfBounds(bs1, 0, 17);
expectSubstringIndexOutOfBounds(bs1, 7, 6);
}
use of com.google.protobuf.ByteString in project j2objc by google.
the class ByteStringTest method testToString.
public void testToString() throws Exception {
ByteString s1 = ByteString.copyFrom("foo".getBytes());
ByteString s2 = ByteString.copyFrom("你好".getBytes("UTF-8"));
assertEquals("foo", s1.toString("UTF-8"));
assertEquals("你好", s2.toString("UTF-8"));
assertEquals("foo", s1.toStringUtf8());
assertEquals("你好", s2.toStringUtf8());
}
use of com.google.protobuf.ByteString in project j2objc by google.
the class CompatibilityTest method checkGetters.
private void checkGetters(TypicalDataOrBuilder data, FieldDescriptor[] fields) {
assertEquals(42, data.getMyInt());
assertEquals("foo", new String(data.getMyBytes().toByteArray()));
assertEquals(TypicalData.EnumType.VALUE9, data.getMyEnumType());
assertTrue(data.getMyBool());
assertEquals(new Float(43.8), data.getMyFloat());
assertEquals(new Double(44.5), data.getMyDouble());
assertEquals("bar", data.getMyString());
assertEquals(24, data.getMyUint());
assertEquals(4422, data.getMyLong());
assertEquals(2244, data.getMyUlong());
assertEquals(1, data.getRepeatedInt32Count());
assertEquals(72, data.getRepeatedInt32(0));
assertEquals("aaa", data.getRepeatedString(0));
assertEquals("ccc", data.getRepeatedString(1));
assertEquals(1.2, data.getRepeatedDouble(0), 0.0001);
Object result;
result = data.getField(fields[1]);
assertTrue(result instanceof Integer);
assertEquals(42, result);
result = data.getField(fields[2]);
assertTrue(result instanceof ByteString);
assertEquals("foo", new String(((ByteString) result).toByteArray()));
result = data.getField(fields[3]);
assertTrue(result instanceof EnumValueDescriptor);
assertEquals(9, ((EnumValueDescriptor) result).getNumber());
result = data.getField(fields[11]);
assertTrue(result instanceof TypicalDataMessage);
assertEquals(TypicalDataMessage.newBuilder().build(), result);
result = data.getField(fields[12]);
assertTrue(result instanceof Boolean);
assertEquals(Boolean.TRUE, result);
result = data.getField(fields[13]);
assertTrue(result instanceof Float);
assertEquals(43.8, ((Float) result).floatValue(), 0.0001);
result = data.getField(fields[14]);
assertTrue(result instanceof Double);
assertEquals(44.5, ((Double) result).doubleValue(), 0.0001);
result = data.getField(fields[15]);
assertTrue(result instanceof String);
assertEquals("bar", result);
result = data.getField(fields[16]);
assertTrue(result instanceof Integer);
assertEquals(24, result);
result = data.getField(fields[17]);
assertTrue(result instanceof Long);
assertEquals(4422L, result);
result = data.getField(fields[18]);
assertTrue(result instanceof Long);
assertEquals(2244L, result);
assertEquals(1, data.getRepeatedFieldCount(fields[4]));
result = data.getRepeatedField(fields[4], 0);
assertTrue(result instanceof Integer);
assertEquals(72, result);
assertEquals(2, data.getRepeatedFieldCount(fields[8]));
result = data.getRepeatedField(fields[8], 1);
assertEquals("ccc", result);
assertEquals(2, data.getRepeatedFieldCount(fields[7]));
result = data.getRepeatedField(fields[7], 1);
assertEquals(3.4, ((Double) result).doubleValue(), 0.0001);
}
use of com.google.protobuf.ByteString in project j2objc by google.
the class CompatibilityTest method testSetAndGetByteString.
public void testSetAndGetByteString() throws Exception {
ByteString bstr = ByteString.copyFrom("foo".getBytes());
TypicalData data = TypicalData.newBuilder().setMyBytes(bstr).build();
assertEquals("foo", new String(data.getMyBytes().toByteArray()));
}
use of com.google.protobuf.ByteString in project j2objc by google.
the class CompatibilityTest method testSetAndGetRepeatedBytes.
public void testSetAndGetRepeatedBytes() throws Exception {
List<ByteString> list = new ArrayList<ByteString>();
list.add(ByteString.copyFrom("def".getBytes()));
list.add(ByteString.copyFrom("ghi".getBytes()));
TypicalData data = TypicalData.newBuilder().addRepeatedBytes(ByteString.copyFrom("abc".getBytes())).addAllRepeatedBytes(list).setRepeatedBytes(2, ByteString.copyFrom("jkl".getBytes())).build();
assertEquals(3, data.getRepeatedBytesCount());
assertEquals("abc", new String(data.getRepeatedBytes(0).toByteArray()));
byte[] bytes = data.toByteArray();
TypicalData other = TypicalData.parseFrom(bytes);
assertEquals("abc", new String(other.getRepeatedBytes(0).toByteArray()));
assertEquals("def", new String(other.getRepeatedBytesList().get(1).toByteArray()));
assertEquals("jkl", new String(other.getRepeatedBytes(2).toByteArray()));
}
Aggregations