Skip to content

Commit

Permalink
get_bit: Added test cases.
Browse files Browse the repository at this point in the history
+ Added test cases for get_bit.
+ get_bit declared as inline function instead of static inline
  to allow for testing in test_geohash.c
  • Loading branch information
Emmanuel Odeke committed Oct 30, 2014
1 parent 7dc0ada commit 3bd1f00
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
3 changes: 1 addition & 2 deletions geohash.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ int geohash_encode(
return 0;
}

static inline uint8_t get_bit(uint64_t bits, uint8_t pos)
inline uint8_t get_bit(uint64_t bits, uint8_t pos)
{
// TODO: Confirm with @yinqiwen if we should instead be using [ bits & (1 << pos) ]
return (bits >> pos) & 0x01;
}

Expand Down
1 change: 1 addition & 0 deletions geohash.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ extern "C"
GeoHashBits geohash_next_lefttop(GeoHashBits bits);
GeoHashBits geohash_next_righttop(GeoHashBits bits);

inline uint8_t get_bit(uint64_t bits, uint8_t pos);

#if defined(__cplusplus)
}
Expand Down
35 changes: 35 additions & 0 deletions test-geohash.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
#include <time.h>
#include <stdio.h>
#include <assert.h>
#include <sys/time.h>

#include "geohash.h"

struct Tupled {
uint64_t n;
uint8_t i;
uint8_t expected:1;
} get_bit_test_cases[] = {
{.n = -1, .i = 3, .expected = 1}, // .n = 0b1111111111111111111111111111111111111111111111111111111111111111
{.n = 99, .i = 3, .expected = 0}, // .n = 0b0000000000000000000000000000000000000000000000000000000001100011
{.n = 512, .i = 9, .expected = 1}, // .n = 0b0000000000000000000000000000000000000000000000000000001000000000
{.n = 512, .i = 19, .expected = 0}, // .n = 0b0000000000000000000000000000000000000000000000000000001000000000
{.n = 23457162, .i = 3, .expected = 1}, // .n = 0b0000000000000000000000000000000000000001011001011110110110001010
{.n = 293457162, .i = 255, .expected = 0}, // .n = 0b0000000000000000000000000000000000b10001011111011100110100001010
{.n = 1, .i = 3, .expected = 0}, // .n = 0b0000000000000000000000000000000000000000000000000000000000000001
{.n = 1, .i = 0, .expected = 1}, // .n = 0b0000000000000000000000000000000000000000000000000000000000000001
{.n = 0, .i = 1, .expected = 0}, // .n = 0b0000000000000000000000000000000000000000000000000000000000000000
{.n = 0, .i = 10, .expected = 0}, // .n = 0b0000000000000000000000000000000000000000000000000000000000000000
};

void test_get_bit() {
uint8_t result;
int i, t_len = sizeof(get_bit_test_cases)/sizeof(get_bit_test_cases[0]);

struct Tupled cur;

for (i = 0; i < t_len; ++i) {
cur = get_bit_test_cases[i];
result = get_bit(cur.n, cur.i);
assert(cur.expected == result);
}

printf("Passed test_get_bit\n");
}

uint64_t get_current_epoch_millis()
{
struct timeval timeValue;
Expand Down Expand Up @@ -116,7 +149,9 @@ int test_2()

return 0;
}

int main() {
test_get_bit();
test_1();
test_2();
return 0;
Expand Down

0 comments on commit 3bd1f00

Please sign in to comment.