Skip to content

Commit

Permalink
Format - Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jocic committed Jul 10, 2023
1 parent 6618520 commit 042e00a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
2 changes: 2 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
* - Add unit tests - covering everything
* - Alter the code removing use of any data types larger than a single byte
* - Replace "get/set" functions with macro functions
* - Buffered sample read functions - not sample by sample but 64 128, etc
* - Document everything
* - Update readme file including examples
* - Use error codes
* - Release version 1.0.0
*/

Expand Down
42 changes: 39 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Following examples should cover all basic use-case scenarios.
### Opening & Closing

```c
WAV_FILE file = wav_open("/path/to/file.wav", BIN_RD);
WAV_FILE file = wav_open("/path/to/file.wav", WAV_RD);

if (file.open) {

Expand All @@ -26,7 +26,7 @@ if (file.open) {
### WAV Header

```c
WAV_FILE file = wav_open("/path/to/file.wav", BIN_RD);
WAV_FILE file = wav_open("/path/to/file.wav", WAV_RD);

if (file.open) {

Expand All @@ -53,7 +53,7 @@ if (file.open) {
### Reading Samples

```c
WAV_FILE file = wav_open("/path/to/file.wav", BIN_RD);
WAV_FILE file = wav_open("/path/to/file.wav", WAV_RD);

if (file.open) {

Expand All @@ -74,6 +74,42 @@ if (file.open) {
}
```

### Writing Samples

```
WAV_FILE file = wav_open("/path/to/file.wav", WAV_NEW);
if (file.open) {
int16_t sample_high = (pow(2, 15) - 1);
int16_t sample_low = sample_high * -1;
int duration = 5;
int tone_frequency = 1000;
int samples_per_period = 44100 / tone_frequency;
int total_samples = duration * 44100;
bool pos_period = false;
for (int i = 0; i < total_samples; i++) {
if (pos_period) {
wav_push_sample(&file, sample_high);
} else {
wav_push_sample(&file, sample_low);
}
if ((i % samples_per_period) == 0) {
pos_period = !pos_period;
}
}
if (!wav_close(&file)) {
// Handle I/O Error
}
}
```

## Versioning Scheme

I use a 3-digit [Semantic Versioning](https://semver.org/spec/v2.0.0.html) identifier, for example 1.0.2. These digits have the following meaning:
Expand Down
23 changes: 22 additions & 1 deletion tests/wav-format.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>

#include "format.h"
#include "wav-format.h"
Expand Down Expand Up @@ -98,10 +99,30 @@ void test_wav_write() {

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

WAV_FILE file = wav_open(MAIN_TEST_FILE, WAV_READ);
WAV_FILE file = wav_open("test-files/test-tone.wav", WAV_NEW);
assert(file.bin.open && "File couldn't be opened.");

wav_set_defaults(&file);

int val_high = pow(2, 15) - 1;
int val_low = val_high * -1;

printf("WTF: %d %d\n", val_high, val_low);

int i, j;

for (i = 0, j = 0; i < 44100 * 5; i++) {

if (j) {
assert(wav_push_sample(&file, val_high) && "Couldn't push a sample: HIGH");
} else {
assert(wav_push_sample(&file, val_low) && "Couldn't push a sample: LOW");
}

if ((i % 133) == 0) {
j = !j;
}
}

wav_close(&file);
assert(!file.bin.open && "File couldn't be closed.");
Expand Down

0 comments on commit 042e00a

Please sign in to comment.