Skip to content

Commit

Permalink
Push unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jocic committed Jul 20, 2023
1 parent de58713 commit a52a3a3
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 51 deletions.
72 changes: 34 additions & 38 deletions source/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,6 @@ uint32_t wav_sample_count(WAV_FILE *wf) {
return sample_count;
}

int32_t wav_get_sample(WAV_FILE *wf, uint32_t n) {

int32_t sample = 0;

wav_get_1ch_sample(wf, n, &sample);

return sample;
}

void wav_get_1ch_sample(WAV_FILE *wf, uint32_t n, void* val) {

uint8_t bps = wav_get_BitsPerSample(wf);
Expand Down Expand Up @@ -176,17 +167,43 @@ bool wav_set_2ch_sample(WAV_FILE *wf, uint32_t n, void* lval, void* rval) {
&& bin_w8(&wf->bin, off_rch, *((int8_t*)rval));
case 16:
return bin_w16l(&wf->bin, off_lch, *((int16_t*)lval))
&& bin_w16l(&wf->bin, off_rch, *((int8_t*)rval));
&& bin_w16l(&wf->bin, off_rch, *((int16_t*)rval));
case 32:
return bin_w32l(&wf->bin, off_lch, *((int32_t*)lval))
&& bin_w32l(&wf->bin, off_rch, *((int8_t*)rval));
&& bin_w32l(&wf->bin, off_rch, *((int32_t*)rval));
}

wf->err = WAV_ERR_SET_SAMPLE;

return false;
}

bool wav_push_1ch_sample(WAV_FILE *wf, void* val) {

if (wav_set_1ch_sample(wf, wf->curr, val)) {

wf->curr++;
wf->alt = true;

return true;
}

return false;
}

bool wav_push_2ch_sample(WAV_FILE *wf, void* lval, void* rval) {

if (wav_set_2ch_sample(wf, wf->curr, lval, rval)) {

wf->curr++;
wf->alt = true;

return true;
}

return false;
}

bool wav_set_psample(WAV_FILE *wf, int32_t val) {
return wav_set_sample(wf, wf->curr - 1, &val);
}
Expand All @@ -204,41 +221,20 @@ bool wav_has_next(WAV_FILE *wf) {
}

int32_t wav_next_sample(WAV_FILE *wf) {
return wav_get_sample(wf, wf->curr++);
return 0;//wav_get_sample(wf, wf->curr++);
}

void wav_next_1ch_sample(WAV_FILE *wf, int32_t* val) {
*val = wav_get_sample(wf, wf->curr++);
//*val = wav_get_sample(wf, wf->curr++);
}

void wav_next_2ch_sample(WAV_FILE *wf, int32_t* lval, int32_t* rval) {

int32_t left = wav_get_sample(wf, wf->curr++);
int32_t right = wav_get_sample(wf, wf->curr++);
//int32_t left = wav_get_sample(wf, wf->curr++);
//int32_t right = wav_get_sample(wf, wf->curr++);

*lval = left;
*rval = right;
}

bool wav_push_sample(WAV_FILE *wf, int32_t val) {

if (wav_set_sample(wf, wf->curr, &val)) {

wf->curr++;
wf->alt = true;

return true;
}

return false;
}

bool wav_push_1ch_sample(WAV_FILE *wf, int32_t val) {
return wav_push_sample(wf, val);
}

bool wav_push_2ch_sample(WAV_FILE *wf, int32_t lval, int32_t rval) {
return wav_push_sample(wf, lval) && wav_push_sample(wf, rval);
//*lval = left;
//*rval = right;
}

bool wav_is_valid(WAV_FILE *wf) {
Expand Down
11 changes: 7 additions & 4 deletions source/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
#define wav_rewind(wf) ((*wf).curr = 0)
#define wav_last_error(wf) ((*wf).err)
#define wav_set_defaults(wf) (wav_set_1ch_defaults(wf))
#define wav_get_sample(wf, n, v) (wav_get_1ch_sample(wf, n, v))
#define wav_set_sample(wf, n, v) (wav_set_1ch_sample(wf, n, v))
#define wav_push_sample(wf, v) (wav_push_1ch_sample(wf, v))

#define wav_get_ChunkID(wf) (bin_r32b(&(*wf).bin, 0))
#define wav_get_ChunkSize(wf) (bin_r32l(&(*wf).bin, 4))
Expand Down Expand Up @@ -77,21 +79,22 @@
bool wav_commit(WAV_FILE *wf);
uint32_t wav_est_duration(WAV_FILE *wf);
uint32_t wav_sample_count(WAV_FILE *wf);
int32_t wav_get_sample(WAV_FILE *wf, uint32_t n);
void wav_get_1ch_sample(WAV_FILE *wf, uint32_t n, void* val);
void wav_get_2ch_sample(WAV_FILE *wf, uint32_t n, void* lval, void* rval);
bool wav_set_1ch_sample(WAV_FILE *wf, uint32_t n, void* val);
bool wav_set_2ch_sample(WAV_FILE *wf, uint32_t n, void* lval, void* rval);
bool wav_push_1ch_sample(WAV_FILE *wf, void* val);
bool wav_push_2ch_sample(WAV_FILE *wf, void* lval, void* rval);

bool wav_set_psample(WAV_FILE *wf, int32_t val);
bool wav_set_1ch_psample(WAV_FILE *wf, int32_t val);
bool wav_set_2ch_psample(WAV_FILE *wf, int32_t lval, int32_t rval);

bool wav_has_next(WAV_FILE *wf);
int32_t wav_next_sample(WAV_FILE *wf);
void wav_next_1ch_sample(WAV_FILE *wf, int32_t* val);
void wav_next_2ch_sample(WAV_FILE *wf, int32_t* lval, int32_t* rval);
bool wav_push_sample(WAV_FILE *wf, int32_t val);
bool wav_push_1ch_sample(WAV_FILE *wf, int32_t val);
bool wav_push_2ch_sample(WAV_FILE *wf, int32_t lval, int32_t rval);

bool wav_is_valid(WAV_FILE *wf);
bool wav_set_1ch_defaults(WAV_FILE *wf);
bool wav_set_2ch_defaults(WAV_FILE *wf);
Expand Down
10 changes: 5 additions & 5 deletions tests/functionality.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ void test_func_1ch_mutation() {
for (i = 0; i < total_samples; i++) {

if (tone_hstate) {
wav_push_1ch_sample(&file, tone_high);
wav_push_1ch_sample(&file, &tone_high);
} else {
wav_push_1ch_sample(&file, tone_low);
wav_push_1ch_sample(&file, &tone_low);
}

if ((i % tone_spp) == 0) {
Expand Down Expand Up @@ -76,7 +76,7 @@ void test_func_2ch_mutation() {

for (i = 0; i < total_samples; i++) {

wav_push_2ch_sample(&file, *tone1_current, *tone2_current);
wav_push_2ch_sample(&file, tone1_current, tone2_current);

if ((i % tone1_spp) == 0) {

Expand Down Expand Up @@ -127,9 +127,9 @@ void test_func_1ch_edit() {
for (smp_ptr = 0; smp_ptr < total_samples; smp_ptr++) {

if (tone_state) {
wav_push_sample(&file, tone_high);
wav_push_sample(&file, &tone_high);
} else {
wav_push_sample(&file, tone_low);
wav_push_sample(&file, &tone_low);
}

if ((smp_ptr % samples_per_period) == 0) {
Expand Down
80 changes: 76 additions & 4 deletions tests/wav-format.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ void test_wav_commit() {
assert(wav_sample_count(&file) == 0 && "Invalid smaple count.");

uint32_t i;
uint16_t dummy_sample = 0xAA;

for (i = 0; i < 44100; i++) {
wav_push_1ch_sample(&file, 0xAA);
wav_push_1ch_sample(&file, &dummy_sample);
}

assert(wav_commit(&file) && "File couldn't be commited.");
Expand Down Expand Up @@ -95,9 +96,10 @@ void test_wav_sugar() {
assert(file.curr == 0 && "Invalid pointer.");

uint32_t i;
uint16_t dummy_sample = 0xAA;

for (i = 0; i < 44100; i++) {
wav_push_1ch_sample(&file, 0xAA);
wav_push_1ch_sample(&file, &dummy_sample);
}

assert(wav_is_altered(&file) && "Altered - false flag.");
Expand Down Expand Up @@ -241,8 +243,8 @@ void test_wav_get() {

for (i = 0; i < 6; i++) {

assert((wav_get_sample(&file_1ch, samples_1ch[i].num) ==
samples_1ch[i].lval) && "Invalid sample value.");
wav_get_sample(&file_1ch, samples_1ch[i].num, &lval);
assert((lval == samples_1ch[i].lval) && "Invalid sample value.");

wav_get_1ch_sample(&file_1ch, samples_1ch[i].num, &lval);
assert((lval == samples_1ch[i].lval) && "Invalid sample value.");
Expand Down Expand Up @@ -343,6 +345,75 @@ void test_wav_set() {
assert(wav_last_error(&file_2ch) == WAV_ERR_NONE && "Error flag set.");
}

void test_wav_push() {

struct test_sample {
int16_t lval;
int16_t rval;
};

uint32_t i;
int16_t lval, rval;

const struct test_sample samples_1ch[6] = {
{ 0x3D73, 0x0000 }, { 0x6E8F, 0x0000 }, { 0x6183, 0x0000 },
{ 0x66E7, 0x0000 }, { 0x65E7, 0x0000 }, { 0x66E1, 0x0000 }
};

const struct test_sample samples_2ch[6] = {
{ 0x6E8F, 0x3D73 }, { 0x0001, 0x66E1 },
{ 0x3D73, 0xFFFE }, { 0x0004, 0x0002 },
{ 0xFFFE, 0xFFFD }, { 0xFFFD, 0x0002 }
};

printf("[*] TEST: WAV Format -> Push\n");

// 1 Channel

WAV_FILE file_1ch = wav_open("test-files/wav-format/1ch-push-test.wav", WAV_NEW);

assert(file_1ch.bin.open && "File couldn't be opened.");
assert(wav_last_error(&file_1ch) == WAV_ERR_NONE && "Error flag set.");

assert(wav_set_1ch_defaults(&file_1ch) && "Couldn't set defaults.");

for (i = 0; i < 6; i++) {

lval = samples_1ch[i].lval;
assert(wav_push_1ch_sample(&file_1ch, &lval) && "Couldn't set a sample.");

wav_get_1ch_sample(&file_1ch, i, &lval);
assert((lval == samples_1ch[i].lval) && "Invalid sample value.");
}

assert(wav_close(&file_1ch) && "File couldn't be closed.");
assert(wav_last_error(&file_1ch) == WAV_ERR_NONE && "Error flag set.");

// 2 Channel

WAV_FILE file_2ch = wav_open("test-files/wav-format/2ch-push-test.wav", WAV_NEW);

assert(file_2ch.bin.open && "File couldn't be opened.");
assert(wav_last_error(&file_2ch) == WAV_ERR_NONE && "Error flag set.");

assert(wav_set_2ch_defaults(&file_2ch) && "Couldn't set defaults.");

for (i = 0; i < 6; i++) {

lval = samples_2ch[i].lval;
rval = samples_2ch[i].rval;
assert(wav_push_2ch_sample(&file_2ch, &lval, &rval) && "Couldn't set a sample.");

wav_get_2ch_sample(&file_2ch, i, &lval, &rval);
assert((lval == samples_2ch[i].lval) && "Invalid sample value.");
assert((rval == samples_2ch[i].rval) && "Invalid sample value.");
}

assert(wav_close(&file_2ch) && "File couldn't be closed.");
assert(wav_last_error(&file_2ch) == WAV_ERR_NONE && "Error flag set.");

}

void test_wav_format() {

test_wav_open();
Expand All @@ -353,4 +424,5 @@ void test_wav_format() {
test_wav_defaults();
test_wav_get();
test_wav_set();
test_wav_push();
}

0 comments on commit a52a3a3

Please sign in to comment.