Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
72776c30
Commit
72776c30
authored
Feb 19, 2015
by
Jisi Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tests for oneofs.
parent
96815032
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
142 additions
and
6 deletions
+142
-6
NanoTest.java
...nano/src/test/java/com/google/protobuf/nano/NanoTest.java
+142
-6
No files found.
javanano/src/test/java/com/google/protobuf/nano/NanoTest.java
View file @
72776c30
...
...
@@ -3663,18 +3663,154 @@ public class NanoTest extends TestCase {
}
public
void
testOneof
Basic
()
throws
Exception
{
public
void
testOneof
Default
()
throws
Exception
{
TestAllTypesNano
m1
=
new
TestAllTypesNano
();
checkOneofCase
(
m1
,
0
);
assertEquals
(
WireFormatNano
.
EMPTY_BYTES
,
m1
.
getOneofBytes
());
assertEquals
(
TestAllTypesNano
.
FOO
,
m1
.
getOneofEnum
());
assertEquals
(
0L
,
m1
.
getOneofFixed64
());
assertEquals
(
null
,
m1
.
getOneofNestedMessage
());
assertEquals
(
""
,
m1
.
getOneofString
());
assertEquals
(
0
,
m1
.
getOneofUint32
());
}
public
void
testOneofExclusiveness
()
throws
Exception
{
TestAllTypesNano
m
=
new
TestAllTypesNano
();
checkOneofCase
(
m
,
0
);
m
.
setOneofBytes
(
new
byte
[]{
0
,
1
});
checkOneofCase
(
m
,
TestAllTypesNano
.
ONEOF_BYTES_FIELD_NUMBER
);
assertTrue
(
Arrays
.
equals
(
new
byte
[]{
0
,
1
},
m
.
getOneofBytes
()));
m
.
setOneofEnum
(
TestAllTypesNano
.
BAZ
);
checkOneofCase
(
m
,
TestAllTypesNano
.
ONEOF_ENUM_FIELD_NUMBER
);
assertEquals
(
TestAllTypesNano
.
BAZ
,
m
.
getOneofEnum
());
assertEquals
(
WireFormatNano
.
EMPTY_BYTES
,
m
.
getOneofBytes
());
m1
.
setOneofString
(
"hello"
);
checkOneofCase
(
m1
,
TestAllTypesNano
.
ONEOF_STRING_FIELD_NUMBER
);
m
.
setOneofFixed64
(-
1L
);
checkOneofCase
(
m
,
TestAllTypesNano
.
ONEOF_FIXED64_FIELD_NUMBER
);
assertEquals
(-
1L
,
m
.
getOneofFixed64
());
assertEquals
(
TestAllTypesNano
.
FOO
,
m
.
getOneofEnum
());
m
.
setOneofNestedMessage
(
new
TestAllTypesNano
.
NestedMessage
());
checkOneofCase
(
m
,
TestAllTypesNano
.
ONEOF_NESTED_MESSAGE_FIELD_NUMBER
);
assertEquals
(
new
TestAllTypesNano
.
NestedMessage
(),
m
.
getOneofNestedMessage
());
assertEquals
(
0L
,
m
.
getOneofFixed64
());
m
.
setOneofString
(
"hello"
);
checkOneofCase
(
m
,
TestAllTypesNano
.
ONEOF_STRING_FIELD_NUMBER
);
assertEquals
(
"hello"
,
m
.
getOneofString
());
assertNull
(
m
.
getOneofNestedMessage
());
m
.
setOneofUint32
(
10
);
checkOneofCase
(
m
,
TestAllTypesNano
.
ONEOF_UINT32_FIELD_NUMBER
);
assertEquals
(
10
,
m
.
getOneofUint32
());
assertEquals
(
""
,
m
.
getOneofString
());
m
.
setOneofBytes
(
new
byte
[]{
0
,
1
});
checkOneofCase
(
m
,
TestAllTypesNano
.
ONEOF_BYTES_FIELD_NUMBER
);
assertTrue
(
Arrays
.
equals
(
new
byte
[]{
0
,
1
},
m
.
getOneofBytes
()));
assertEquals
(
0
,
m
.
getOneofUint32
());
}
public
void
testOneofClear
()
throws
Exception
{
TestAllTypesNano
m
=
new
TestAllTypesNano
();
m
.
setOneofBytes
(
new
byte
[]{
0
,
1
});
m
.
clearOneofField
();
checkOneofCase
(
m
,
0
);
m
.
setOneofEnum
(
TestAllTypesNano
.
BAZ
);
m
.
clearOneofField
();
checkOneofCase
(
m
,
0
);
m
.
setOneofFixed64
(-
1L
);
m
.
clearOneofField
();
checkOneofCase
(
m
,
0
);
m
.
setOneofNestedMessage
(
new
TestAllTypesNano
.
NestedMessage
());
m
.
clearOneofField
();
checkOneofCase
(
m
,
0
);
m
.
setOneofString
(
"hello"
);
m
.
clearOneofField
();
checkOneofCase
(
m
,
0
);
m
.
setOneofUint32
(
10
);
m
.
clearOneofField
();
checkOneofCase
(
m
,
0
);
}
public
void
testOneofMarshling
()
throws
Exception
{
TestAllTypesNano
m
=
new
TestAllTypesNano
();
TestAllTypesNano
parsed
=
new
TestAllTypesNano
();
{
m
.
setOneofBytes
(
new
byte
[]{
0
,
1
});
byte
[]
serialized
=
MessageNano
.
toByteArray
(
m
);
MessageNano
.
mergeFrom
(
parsed
,
serialized
);
checkOneofCase
(
parsed
,
TestAllTypesNano
.
ONEOF_BYTES_FIELD_NUMBER
);
assertTrue
(
Arrays
.
equals
(
new
byte
[]{
0
,
1
},
parsed
.
getOneofBytes
()));
}
{
m
.
setOneofEnum
(
TestAllTypesNano
.
BAZ
);
byte
[]
serialized
=
MessageNano
.
toByteArray
(
m
);
MessageNano
.
mergeFrom
(
parsed
,
serialized
);
checkOneofCase
(
m
,
TestAllTypesNano
.
ONEOF_ENUM_FIELD_NUMBER
);
assertEquals
(
TestAllTypesNano
.
BAZ
,
m
.
getOneofEnum
());
}
{
m
.
setOneofEnum
(
TestAllTypesNano
.
BAZ
);
byte
[]
serialized
=
MessageNano
.
toByteArray
(
m
);
MessageNano
.
mergeFrom
(
parsed
,
serialized
);
checkOneofCase
(
m
,
TestAllTypesNano
.
ONEOF_ENUM_FIELD_NUMBER
);
assertEquals
(
TestAllTypesNano
.
BAZ
,
m
.
getOneofEnum
());
}
{
m
.
setOneofFixed64
(-
1L
);
byte
[]
serialized
=
MessageNano
.
toByteArray
(
m
);
MessageNano
.
mergeFrom
(
parsed
,
serialized
);
checkOneofCase
(
m
,
TestAllTypesNano
.
ONEOF_FIXED64_FIELD_NUMBER
);
assertEquals
(-
1L
,
m
.
getOneofFixed64
());
}
{
m
.
setOneofNestedMessage
(
new
TestAllTypesNano
.
NestedMessage
());
byte
[]
serialized
=
MessageNano
.
toByteArray
(
m
);
MessageNano
.
mergeFrom
(
parsed
,
serialized
);
checkOneofCase
(
m
,
TestAllTypesNano
.
ONEOF_NESTED_MESSAGE_FIELD_NUMBER
);
assertEquals
(
new
TestAllTypesNano
.
NestedMessage
(),
m
.
getOneofNestedMessage
());
}
{
m
.
setOneofString
(
"hello"
);
byte
[]
serialized
=
MessageNano
.
toByteArray
(
m
);
MessageNano
.
mergeFrom
(
parsed
,
serialized
);
assertEquals
(
"hello"
,
m
.
getOneofString
());
assertNull
(
m
.
getOneofNestedMessage
());
}
{
m
.
setOneofUint32
(
10
);
byte
[]
serialized
=
MessageNano
.
toByteArray
(
m
);
MessageNano
.
mergeFrom
(
parsed
,
serialized
);
checkOneofCase
(
m
,
TestAllTypesNano
.
ONEOF_UINT32_FIELD_NUMBER
);
assertEquals
(
10
,
m
.
getOneofUint32
());
}
}
public
void
testOneofSerializedConcat
()
throws
Exception
{
TestAllTypesNano
m1
=
new
TestAllTypesNano
();
m1
.
setOneofBytes
(
new
byte
[]
{
0
,
1
});
byte
[]
b1
=
MessageNano
.
toByteArray
(
m1
);
TestAllTypesNano
m2
=
new
TestAllTypesNano
();
m2
.
setOneofEnum
(
TestAllTypesNano
.
BAZ
);
assertTrue
(
m2
.
hasOneofEnum
());
assertEquals
(
TestAllTypesNano
.
ONEOF_ENUM_FIELD_NUMBER
,
m2
.
getOneofFieldCase
());
byte
[]
b2
=
MessageNano
.
toByteArray
(
m2
);
byte
[]
b3
=
new
byte
[
b1
.
length
+
b2
.
length
];
System
.
arraycopy
(
b1
,
0
,
b3
,
0
,
b1
.
length
);
System
.
arraycopy
(
b2
,
0
,
b3
,
b1
.
length
,
b2
.
length
);
TestAllTypesNano
parsed
=
new
TestAllTypesNano
();
MessageNano
.
mergeFrom
(
parsed
,
b3
);
// the last on the wire wins.
checkOneofCase
(
parsed
,
TestAllTypesNano
.
ONEOF_ENUM_FIELD_NUMBER
);
assertEquals
(
TestAllTypesNano
.
BAZ
,
parsed
.
getOneofEnum
());
}
public
void
testNullRepeatedFields
()
throws
Exception
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment