summaryrefslogtreecommitdiff
path: root/runtime.c
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2025-04-27 12:59:53 +0200
committerMitya Selivanov <automainint@guattari.tech>2025-04-27 12:59:53 +0200
commitd033c731fffac4e3a83e324779ab06ecded38977 (patch)
tree2abe89e3b942a82c438144511065542be37f2a86 /runtime.c
parent7aae9cbf21872804b29c72e753850aca387ffe7c (diff)
downloadreduced_system_layer-d033c731fffac4e3a83e324779ab06ecded38977.zip
File formats tests
Diffstat (limited to 'runtime.c')
-rw-r--r--runtime.c192
1 files changed, 183 insertions, 9 deletions
diff --git a/runtime.c b/runtime.c
index 09cf7a1..32c9766 100644
--- a/runtime.c
+++ b/runtime.c
@@ -723,8 +723,8 @@ b8 bmp_read_from_memory(i64 *width, i64 *height, i64 colors_len, vec4_f32 *color
b8 bmp_write_to_memory (i64 *dst_len, u8 *dst, i64 width, i64 height, i64 stride, vec4_f32 *colors);
b8 ppm_read_from_memory(i64 *width, i64 *height, i64 colors_len, vec4_f32 *colors, i64 src_len, u8 *src);
b8 ppm_write_to_memory (i64 *dst_len, u8 *dst, i64 width, i64 height, i64 stride, vec4_f32 *colors);
-b8 wav_read_from_memory(i64 *num_channels, i64 *sample_rate, i64 *num_samples, i64 frames_len, f32 *frames, i64 src_len, u8 *src);
-b8 wav_write_to_memory (i64 *dst_len, u8 *dst, i64 num_channels, i64 sample_rate, i64 num_samples, f32 *frames);
+b8 wav_read_from_memory(i64 *sample_rate_hz, i64 *num_channels, i64 *num_samples, i64 frames_len, f32 *frames, i64 src_len, u8 *src);
+b8 wav_write_to_memory (i64 *dst_len, u8 *dst, i64 sample_rate_hz, i64 num_channels, i64 num_samples, f32 *frames);
// Dynamic libraries
void dynamic_library_open (u16 slot, c8 *name);
@@ -2403,11 +2403,11 @@ b8 ppm_write_to_memory(i64 *dst_len, u8 *dst, i64 width, i64 height, i64 stride,
return 0;
}
-b8 wav_read_from_memory(i64 *num_channels, i64 *sample_rate, i64 *num_samples, i64 frames_len, f32 *frames, i64 src_len, u8 *src) {
+b8 wav_read_from_memory(i64 *sample_rate_hz, i64 *num_channels, i64 *num_samples, i64 frames_len, f32 *frames, i64 src_len, u8 *src) {
// TODO
+ (void) sample_rate_hz;
(void) num_channels;
- (void) sample_rate;
(void) num_samples;
(void) frames_len;
(void) frames;
@@ -2418,13 +2418,13 @@ b8 wav_read_from_memory(i64 *num_channels, i64 *sample_rate, i64 *num_samples, i
return 0;
}
-b8 wav_write_to_memory(i64 *dst_len, u8 *dst, i64 num_channels, i64 sample_rate, i64 num_samples, f32 *frames) {
+b8 wav_write_to_memory(i64 *dst_len, u8 *dst, i64 sample_rate_hz, i64 num_channels, i64 num_samples, f32 *frames) {
// TODO
(void) dst_len;
(void) dst;
(void) num_channels;
- (void) sample_rate;
+ (void) sample_rate_hz;
(void) num_samples;
(void) frames;
@@ -5427,15 +5427,189 @@ TEST("average frame duration") {
}
TEST("BMP image") {
- // TODO
+ i64 width = 800;
+ i64 height = 600;
+ i64 stride = width;
+
+ i64 pixels_len = 0;
+ vec4_f32 *pixels = NULL;
+ resize_dynamic_array_exact(&pixels_len, (void **) &pixels, sizeof *pixels, width * height);
+ REQUIRE_EQ(pixels_len, width * height);
+
+ for (i64 j = 0; j < height; ++j)
+ for (i64 i = 0; i < width; ++i)
+ pixels[j * stride + i] = (vec4_f32) {
+ (f64) i / (f64) (width - 1),
+ (f64) j / (f64) (height - 1),
+ (f64) (i + j) / (f64) (width + height - 2)
+ };
+
+ i64 num_bytes = 0;
+ i64 bytes_len = 0;
+ u8 *bytes = NULL;
+
+ REQUIRE(bmp_write_to_memory(&num_bytes, NULL, width, height, stride, pixels));
+ REQUIRE(num_bytes != 0);
+
+ mem_set_(pixels, 0, pixels_len * sizeof *pixels);
+
+ resize_dynamic_array_exact(&bytes_len, (void **) &bytes, sizeof *bytes, num_bytes);
+ REQUIRE_EQ(bytes_len, num_bytes);
+
+ REQUIRE(bmp_write_to_memory(&num_bytes, bytes, width, height, stride, pixels));
+ REQUIRE_EQ(num_bytes, bytes_len);
+
+ i64 read_width = 0;
+ i64 read_height = 0;
+
+ REQUIRE(bmp_read_from_memory(&read_width, &read_height, pixels_len, pixels, num_bytes, bytes));
+ REQUIRE_EQ(read_width, width);
+ REQUIRE_EQ(read_height, height);
+
+ b8 ok = 1;
+
+ for (i64 j = 0; j < height; ++j)
+ for (i64 i = 0; i < width; ++i)
+ if (pixels[j * stride + i].x < (f64) i / (f64) (width - 1) - 1e-7
+ || pixels[j * stride + i].y < (f64) j / (f64) (height - 1) - 1e-7
+ || pixels[j * stride + i].z < (f64) (i + j) / (f64) (width + height - 2) - 1e-7
+ || pixels[j * stride + i].x > (f64) i / (f64) (width - 1) + 1e-7
+ || pixels[j * stride + i].y > (f64) j / (f64) (height - 1) + 1e-7
+ || pixels[j * stride + i].z > (f64) (i + j) / (f64) (width + height - 2) + 1e-7) {
+ ok = 0;
+ break;
+ }
+
+ REQUIRE(ok);
+
+ resize_dynamic_array_exact(&pixels_len, (void **) &pixels, sizeof *pixels, 0);
+ resize_dynamic_array_exact(&bytes_len, (void **) &bytes, sizeof *bytes, 0);
}
TEST("PPM image") {
- // TODO
+ i64 width = 800;
+ i64 height = 600;
+ i64 stride = width;
+
+ i64 pixels_len = 0;
+ vec4_f32 *pixels = NULL;
+ resize_dynamic_array_exact(&pixels_len, (void **) &pixels, sizeof *pixels, width * height);
+ REQUIRE_EQ(pixels_len, width * height);
+
+ for (i64 j = 0; j < height; ++j)
+ for (i64 i = 0; i < width; ++i)
+ pixels[j * stride + i] = (vec4_f32) {
+ (f64) i / (f64) (width - 1),
+ (f64) j / (f64) (height - 1),
+ (f64) (i + j) / (f64) (width + height - 2)
+ };
+
+ i64 num_bytes = 0;
+ i64 bytes_len = 0;
+ u8 *bytes = NULL;
+
+ REQUIRE(ppm_write_to_memory(&num_bytes, NULL, width, height, stride, pixels));
+ REQUIRE(num_bytes != 0);
+
+ mem_set_(pixels, 0, pixels_len * sizeof *pixels);
+
+ resize_dynamic_array_exact(&bytes_len, (void **) &bytes, sizeof *bytes, num_bytes);
+ REQUIRE_EQ(bytes_len, num_bytes);
+
+ REQUIRE(ppm_write_to_memory(&num_bytes, bytes, width, height, stride, pixels));
+ REQUIRE_EQ(num_bytes, bytes_len);
+
+ i64 read_width = 0;
+ i64 read_height = 0;
+
+ REQUIRE(ppm_read_from_memory(&read_width, &read_height, pixels_len, pixels, num_bytes, bytes));
+ REQUIRE_EQ(read_width, width);
+ REQUIRE_EQ(read_height, height);
+
+ b8 ok = 1;
+
+ for (i64 j = 0; j < height; ++j)
+ for (i64 i = 0; i < width; ++i)
+ if (pixels[j * stride + i].x < (f64) i / (f64) (width - 1) - 1e-7
+ || pixels[j * stride + i].y < (f64) j / (f64) (height - 1) - 1e-7
+ || pixels[j * stride + i].z < (f64) (i + j) / (f64) (width + height - 2) - 1e-7
+ || pixels[j * stride + i].x > (f64) i / (f64) (width - 1) + 1e-7
+ || pixels[j * stride + i].y > (f64) j / (f64) (height - 1) + 1e-7
+ || pixels[j * stride + i].z > (f64) (i + j) / (f64) (width + height - 2) + 1e-7) {
+ ok = 0;
+ break;
+ }
+
+ REQUIRE(ok);
+
+ resize_dynamic_array_exact(&pixels_len, (void **) &pixels, sizeof *pixels, 0);
+ resize_dynamic_array_exact(&bytes_len, (void **) &bytes, sizeof *bytes, 0);
}
TEST("WAV audio") {
- // TODO
+ i64 num_channels = 2;
+ i64 sample_rate_hz = 4100;
+ i64 duration_msec = 1000;
+
+ i64 num_samples = (duration_msec * sample_rate_hz) / 1000;
+ i64 num_frames = num_samples * num_channels;
+
+ f64 base_frequency_hz = 440.0;
+ f64 phase = 0.1;
+
+ i64 frames_len = 0;
+ f32 *frames = NULL;
+ resize_dynamic_array_exact(&frames_len, (void **) &frames, sizeof *frames, num_frames);
+ REQUIRE_EQ(frames_len, num_frames);
+
+ for (i64 i = 0; i < num_samples; ++i)
+ for (i64 k = 0; k < num_channels; ++k) {
+ f64 t_sec = ((f64) i) / sample_rate_hz;
+ frames[i * num_channels + k] = sin(t_sec * base_frequency_hz + (phase * k) / (2.0 * M_PI));
+ }
+
+ i64 num_bytes = 0;
+ i64 bytes_len = 0;
+ u8 *bytes = NULL;
+
+ REQUIRE(wav_write_to_memory(&num_bytes, NULL, sample_rate_hz, num_channels, num_samples, frames));
+ REQUIRE(num_bytes != 0);
+
+ mem_set_(frames, 0, frames_len* sizeof *frames);
+
+ resize_dynamic_array_exact(&bytes_len, (void **) &bytes, sizeof *bytes, num_bytes);
+ REQUIRE_EQ(bytes_len, num_bytes);
+
+ REQUIRE(wav_write_to_memory(&num_bytes, bytes, sample_rate_hz, num_channels, num_samples, frames));
+ REQUIRE_EQ(num_bytes, bytes_len);
+
+ i64 read_sample_rate_hz;
+ i64 read_num_channels;
+ i64 read_num_samples;
+
+ REQUIRE(wav_read_from_memory(&read_sample_rate_hz, &read_num_channels, &read_num_samples, frames_len, frames, num_bytes, bytes));
+ REQUIRE_EQ(read_sample_rate_hz, sample_rate_hz);
+ REQUIRE_EQ(read_num_channels, num_channels);
+ REQUIRE_EQ(read_num_samples, num_samples);
+
+ b8 ok = 1;
+
+ for (i64 i = 0; i < num_samples; ++i)
+ for (i64 k = 0; k < num_channels; ++k) {
+ f64 t_sec = ((f64) i) / sample_rate_hz;
+ f64 amplitude = sin(t_sec * base_frequency_hz + (phase * k) / (2.0 * M_PI));
+
+ if (frames[i * num_channels + k] < amplitude - 1e-7
+ || frames[i * num_channels + k] > amplitude + 1e-7) {
+ ok = 0;
+ break;
+ }
+ }
+
+ REQUIRE(ok);
+
+ resize_dynamic_array_exact(&frames_len, (void **) &frames, sizeof *frames, 0);
+ resize_dynamic_array_exact(&bytes_len, (void **) &bytes, sizeof *bytes, 0);
}
#undef TEST_FILE