Skip to content

Commit

Permalink
added a simple test
Browse files Browse the repository at this point in the history
  • Loading branch information
typester committed Apr 23, 2012
1 parent 7935e88 commit 15c125c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Makefile
@@ -0,0 +1,16 @@
CC?=gcc
PROVE?=prove

test: build_test
$(PROVE) -e '/bin/sh -c' ./test

build_test: test.o
$(CC) $(LDFLAGS) -o test $^

%.o: %.c
$(CC) $(CFLAGS) -c $<

clean:
rm -f *.o
rm -f test_request
rm -f test_response
46 changes: 46 additions & 0 deletions test.c
@@ -0,0 +1,46 @@
#include <stdio.h>
#include <string.h>

#include "hex.h"

static void plan(int num) {
printf("1..%d\n", num);
}

static void ok(int ok, const char* msg) {
static int testnum = 0;
printf("%s %d - %s\n", ok ? "ok" : "not ok", ++testnum, msg);
}

static void test_encode(void) {
unsigned char binary[] = {
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
};
unsigned char hexstring[sizeof(binary) * 2 + 1];

hex(binary, sizeof(binary), hexstring);

ok(0 == strncmp(hexstring, "0123456789abcdef", 16), "encode ok");
}

static void test_decode(void) {
unsigned char hexstring[] = "0123456789abcdef";
unsigned char binary[strlen(hexstring) / 2];
unsigned char expected[] = {
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
};

unhex(hexstring, strlen(hexstring), binary);

ok(0 == memcmp(binary, expected, strlen(hexstring)/2), "decode ok");
}

int main(void) {
plan(2);

test_encode();
test_decode();

return 0;
}

0 comments on commit 15c125c

Please sign in to comment.