~obeancomputer/bitter

764b3b76d27f8c008e1ac3c459c583b104ccd0a4 — ocsmit 1 year, 2 months ago 927368a
Move tests, fix overflow with I/O due to non null terminated magic string
7 files changed, 69 insertions(+), 20 deletions(-)

M Makefile
M src/bitarr_io.c
M src/bitarr_io.h
A tests/Makefile
A tests/data/bitarr_valid.bit
R src/tests.c => tests/tests.c
R src/tests.h => tests/tests.h
M Makefile => Makefile +2 -7
@@ 29,13 29,8 @@ build: $(OBJECTS)
# Phony means not a "real" target, it doesn't build anything
# The phony target "clean" is used to remove all compiled object files.

test: $(OBJECTS) 
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
	@echo
	@echo ------------------ TEST ------------------
	@./test
	@echo
	-@$(MAKE) -s clean
test:
	make -C ./tests

.PHONY: clean check
.SILENT: clean 

M src/bitarr_io.c => src/bitarr_io.c +3 -3
@@ 24,10 24,10 @@ BitArray* BitArray_open(FILE *fp)
{
    uint8_t l, width;
    uint32_t n; 
    char magic_number[strlen(BIT_MAGIC_NUMBER)]; // 3
    
    char magic_number[strlen(BIT_MAGIC_NUMBER)+1];     

    // Verify it is a correct file
    fread(&magic_number, sizeof(char), strlen(BIT_MAGIC_NUMBER), fp);
    fread(magic_number, sizeof(char), strlen(BIT_MAGIC_NUMBER), fp);
    if (strcmp(magic_number, BIT_MAGIC_NUMBER) != 0) {
        fprintf(stderr, "Incorrect file, magic string does not match\n");
        exit(FILE_ERROR);

M src/bitarr_io.h => src/bitarr_io.h +1 -1
@@ 25,7 25,7 @@
#include <string.h>
#include "bitarr.h"

static const char BIT_MAGIC_NUMBER[] = { 'B', 'I', 'T' };
static const char BIT_MAGIC_NUMBER[] = { 'B', 'I', 'T', '\0'};

/**
 * @brief Save BitArray to disk

A tests/Makefile => tests/Makefile +49 -0
@@ 0,0 1,49 @@
CC = gcc
CFLAGS = -g -Wall -O0 -std=c11 -Wextra -Wwrite-strings \
-Wno-parentheses -Wpedantic -Warray-bounds -Wconversion  -Wstrict-prototypes -Wnewline-eof

# Test for leaks with llvm
# Apple clang does not support sanitize
ifeq ($(CHECKLEAK),1)
	CC=/opt/homebrew/opt/llvm/bin/clang
	CFLAGS += -fsanitize=address -fsanitize=leak
endif
#LDFLAGS = -lm

SOURCES=$(wildcard ./*.c ../src/*.c)
OBJECTS=$(patsubst %.c, %.o, $(SOURCES)) # list *.c -> *.o
TARGET = bin

all: test

# The first target defined in the makefile is the one
# used when make is invoked with no argument. Given the definitions
# above, this Makefile file will build the one named TARGET and
# assume that it depends on all the named OBJECTS files.
$(TARGET) : $(OBJECTS)
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)


# Phony means not a "real" target, it doesn't build anything
# The phony target "clean" is used to remove all compiled object files.

test: $(OBJECTS)
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
	@echo
	@echo ------------------ TEST ------------------
	@./test
	@echo
	-@$(MAKE) -s clean

.PHONY: clean check
.SILENT: clean 
clean:
	rm -rf build $(OBJECTS) $(TESTS) $(TARGET) test
	find . -name "*.gc*" -exec rm {} \;
	rm -rf `find . -name "*.dSYM" -print` # Remove XCode junk


check:
	@echo Files with pontentially dangerous functions.
	@egrep '[^_.>a-zA-Z0-9] (str(n?cpy|n?cat|xfrm|n?dup|str|pbrk|tok|_)\
		|stpn?cpy|a?sn?printf|byte_)' $(SOURCES) || true

A tests/data/bitarr_valid.bit => tests/data/bitarr_valid.bit +0 -0
R src/tests.c => tests/tests.c +14 -9
@@ 1,19 1,22 @@
#include "tests.h"
#include "assert.h"
#include <stdio.h>
#include "bitarr.h"
#include "bitops.h"
#include "bitarr_io.h"
#include "../src/bitarr.h"
#include "../src/bitops.h"
#include "../src/bitarr_io.h"



BEGIN_TESTING

// Data

// -- Data --------------------------------------------------------------------
unsigned int A[10] = { 20, 18, 22, 22, 16, 21, 11, 22, 21, 21 };

/*
* Both of binary representations of b have been flipped since when reading individual bits the most from array A, the least significant bit will be read first from each int.
* Both of binary representations of b have been flipped since when reading 
* individual bits the most from array A, the least significant bit will be read
* first from each int.
*
* 394338780 := 1,1,1,0,1,0,1,1,0,0,0,0,1,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,1,0,0,
* 177586    := 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,0,1,0,


@@ 22,11 25,13 @@ unsigned int B_sig_ordered[64] = {
    0,0,1,0,1,0,1,0,0,1,0,1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,1,0,1,1,1,
    0,1,0,0,1,1,0,1,1,0,1,0,1,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};

unsigned int correct_W[2] = { 3943389780, 177586} ;

BitArray* bit_arr = BitArray_init(A, (sizeof(A)/sizeof(A[0])), 5);

static char bit_arr_fp[] = "./data/bitarr_test.bit";

// ----------------------------------------------------------------------------


TEST("BitArray calloc")
{


@@ 103,7 108,7 @@ TEST("bits read range")

TEST("Write to disk")
{
    FILE *fp = fopen("test.bit", "wb");
    FILE *fp = fopen(bit_arr_fp, "wb");
    BitArray_save(bit_arr, fp);
    fclose(fp);
    printf("✔ BitArray disk write\n");


@@ 111,7 116,7 @@ TEST("Write to disk")

TEST("Read from disk")
{
    FILE *fp = fopen("test.bit", "rb");
    FILE *fp = fopen(bit_arr_fp, "rb");
    BitArray* bit_arr_read = BitArray_open(fp);
    fclose(fp);


R src/tests.h => tests/tests.h +0 -0