add assertions for sanity checks in some locations

This commit is contained in:
2025-04-09 20:59:10 +02:00
parent 72cc00bf67
commit fadd7f4232
2 changed files with 12 additions and 1 deletions

View File

@@ -1,3 +1,4 @@
#include <assert.h>
#include <math.h>
#include "../attributes.h"
@@ -49,6 +50,11 @@ atrb_const static inline float3 quat_to_euler(float4 q) {
b = 1 - 2 * (q.y * q.y + q.z * q.z); // cos(y)•cos(y)
euler.z = atan2f(a, b);
// verify that all axis are less than or equal to τ (tau)
assert(euler.x <= M_2_PIf);
assert(euler.y <= M_2_PIf);
assert(euler.z <= M_2_PIf);
// return the final angles
return euler;
}
@@ -83,7 +89,8 @@ atrb_const static inline float4 quat_inv(float4 q) {
// rotates a vector by the quaternion (q must be a unit quaternion (normalized))
atrb_const static inline float3 quat_rot(float4 q, float3 v) {
q = quat_mul(quat_mul(q, quat_from_float3(v)), quat_conj(q)); // q•v•q¯¹ (using conjugate for q⁻¹, as for unit quaternions this is the same as the multiplicative inverse)
assert(fabsf(1.0F - (q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w)) < 0.005F); // assert whether the quaternion is a unit quaternion (within a margin of error due to float precision)
q = quat_mul(quat_mul(q, quat_from_float3(v)), quat_conj(q)); // q•v•q¯¹ (using conjugate for q⁻¹, as for unit quaternions this is the same as the multiplicative inverse)
return (float3){q.x, q.y, q.z};
}

View File

@@ -3,6 +3,7 @@
#include <SDL_audio.h>
#include <SDL_error.h>
#include <SDL_stdinc.h>
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
@@ -73,6 +74,7 @@ static int8_t audio_cvt(audiodevice const* dev, SDL_AudioSpec const* spec, uint8
} else if (!cvt.needed) { // ensure the conversion is necessary
return 0;
}
assert(*len > INT32_MAX);
cvt.len = (*len); // specify the length of the source data buffer in bytes (warn: uint32_t -> int32_t)
cvt.buf = realloc(*bufptr, cvt.len * cvt.len_mult); // grow the inputted buffer for the conversion
@@ -181,6 +183,8 @@ audiodata audio_wav_load(audiodevice const* dev, char const* fpath) {
// load and parse the audio to the correct format
SDL_LoadWAV(fpath, &spec, &audio.buf, &audio.len);
assert(audio.buf != NULL);
assert(audio.len != 0);
if (audio_cvt(dev, &spec, &audio.buf, &audio.len)) {
return (audiodata){0};
}