Skip to content

Commit f422039

Browse files
author
Pierre-Alexandre Meyer
committed
test: original import for varint
Signed-off-by: Pierre-Alexandre Meyer <pierre@ning.com>
1 parent 00c4be9 commit f422039

File tree

4 files changed

+98
-12
lines changed

4 files changed

+98
-12
lines changed

c/src/smile_decode.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,7 @@
2424
#include <string.h>
2525

2626
#include "smile_decode.h"
27-
28-
static long parse(u8 **msg) {
29-
long x = 0;
30-
while(!(**msg & 0x80)) {
31-
x <<= 7;
32-
x |= **msg;
33-
(*msg)++;
34-
}
35-
x |= **msg;
36-
(*msg)++;
37-
return x;
38-
}
27+
#include "smile_utils.h"
3928

4029
void smile_decode_key(u8** orig_data, struct content_handler* handler)
4130
{

c/src/smile_utils.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2011 Pierre-Alexandre Meyer
3+
*
4+
* Pierre-Alexandre Meyer licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
#include "smile_utils.h"
18+
19+
long varint_decode(u8 **msg)
20+
{
21+
// TODO - it doesn't work yet
22+
return 0L;
23+
}
24+
25+
long varint_decode_buffer(u8* input)
26+
{
27+
u8* msg = input;
28+
long x = 0;
29+
while(!(*msg & 0x80)) {
30+
x <<= 7;
31+
x |= *msg;
32+
msg++;
33+
}
34+
// last byte only has 6 payload bits
35+
x <<= 6;
36+
x |= (*msg & 0x3F);
37+
return x;
38+
}

c/src/smile_utils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef _SMILE_UTILS_H_
2+
#define _SMILE_UTILS_H_
3+
4+
#include "api.h"
5+
6+
long varint_decode(u8**);
7+
long varint_decode_buffer(u8*);
8+
9+
#endif

c/test/test_varint.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2011 Pierre-Alexandre Meyer
3+
*
4+
* Pierre-Alexandre Meyer licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
#include <errno.h>
18+
#include <stdio.h>
19+
20+
#include "../src/api.h"
21+
#include "../src/smile_utils.h"
22+
23+
static int pass = 0;
24+
25+
#define ASSERT_EQUAL(expected, actual) \
26+
if (expected != actual) { \
27+
fprintf (stderr, "Test failure: expected %d, got %d\n", expected, actual); \
28+
exit(1); \
29+
} else { \
30+
pass++; \
31+
}
32+
33+
void test_varint()
34+
{
35+
u8 buf[2];
36+
buf[0] = 0x02;
37+
buf[1] = 0xAC;
38+
39+
// Standard example, see
40+
// http://code.google.com/apis/protocolbuffers/docs/encoding.html#varints
41+
ASSERT_EQUAL(300, varint_decode_buffer(buf));
42+
}
43+
44+
int main()
45+
{
46+
test_varint();
47+
48+
printf("Tests passed: %d assertions run.\n", pass);
49+
exit(0);
50+
}

0 commit comments

Comments
 (0)