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
dc497068
Commit
dc497068
authored
May 13, 2016
by
Joshua Haberman
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1529 from gkraynov/test-redundant-varint
Test redundant varint fields decoding in JS.
parents
5c29835f
5d54a852
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
0 deletions
+52
-0
reader_test.js
js/binary/reader_test.js
+52
-0
No files found.
js/binary/reader_test.js
View file @
dc497068
...
...
@@ -359,6 +359,58 @@ describe('binaryReaderTest', function() {
});
/**
* Tests reading a field from hexadecimal string (format: '08 BE EF').
* @param {Function} readField
* @param {number} expected
* @param {string} hexString
*/
function
doTestHexStringVarint_
(
readField
,
expected
,
hexString
)
{
var
bytesCount
=
(
hexString
.
length
+
1
)
/
3
;
var
bytes
=
new
Uint8Array
(
bytesCount
);
for
(
var
i
=
0
;
i
<
bytesCount
;
i
++
)
{
byte
=
parseInt
(
hexString
.
substring
(
i
*
3
,
i
*
3
+
2
),
16
);
bytes
[
i
]
=
byte
;
}
var
reader
=
jspb
.
BinaryReader
.
alloc
(
bytes
);
reader
.
nextField
();
assertEquals
(
expected
,
readField
.
call
(
reader
));
}
/**
* Tests non-canonical redundant varint decoding.
*/
it
(
'testRedundantVarintFields'
,
function
()
{
assertNotNull
(
jspb
.
BinaryReader
.
prototype
.
readUint32
);
assertNotNull
(
jspb
.
BinaryReader
.
prototype
.
readUint64
);
assertNotNull
(
jspb
.
BinaryReader
.
prototype
.
readSint32
);
assertNotNull
(
jspb
.
BinaryReader
.
prototype
.
readSint64
);
// uint32 and sint32 take no more than 5 bytes
// 08 - field prefix (type = 0 means varint)
doTestHexStringVarint_
(
jspb
.
BinaryReader
.
prototype
.
readUint32
,
12
,
'08 8C 80 80 80 00'
);
// 11 stands for -6 in zigzag encoding
doTestHexStringVarint_
(
jspb
.
BinaryReader
.
prototype
.
readSint32
,
-
6
,
'08 8B 80 80 80 00'
);
// uint64 and sint64 take no more than 10 bytes
// 08 - field prefix (type = 0 means varint)
doTestHexStringVarint_
(
jspb
.
BinaryReader
.
prototype
.
readUint64
,
12
,
'08 8C 80 80 80 80 80 80 80 80 00'
);
// 11 stands for -6 in zigzag encoding
doTestHexStringVarint_
(
jspb
.
BinaryReader
.
prototype
.
readSint64
,
-
6
,
'08 8B 80 80 80 80 80 80 80 80 00'
);
});
/**
* Tests 64-bit fields that are handled as strings.
*/
...
...
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