Files

114 lines
3.4 KiB
C

/* Temperature conversion tests against the recovered CFunctions math. */
#include "mag160c/mag160c.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
static void test_calibrate_band_select(void) {
const uint16_t frame[4] = {1000, 2000, 3000, 4000};
const int16_t thresh[4] = {5, 5, 5, 5};
const uint16_t pwl[16] = {
0, 100, 0, 100, 0, 100, 0, 100,
0, 200, 0, 200, 0, 200, 0, 200,
};
mag160c_temp_tables_t t;
memset(&t, 0, sizeof(t));
t.thresholds = thresh;
t.pwl = pwl;
t.pixel_count = 4;
t.band_count = 2;
uint16_t out[4];
mag160c_temp_calibrate(frame, &t, out);
/* diff = frame>>1 = 500..2000 all > 5 -> band 1 -> 200 */
for (int i = 0; i < 4; ++i) {
assert(out[i] == 200);
}
}
static void test_calibrate_interp_clamp(void) {
const uint16_t frame[2] = {2000, 0x8000};
const uint16_t baseline[2] = {0, 0x100};
const int16_t thresh[2] = {500, 500};
const uint16_t pwl[8] = {
0x1000, 1000, 0x1000, 1000,
0x0100, 2000, 0x0100, 2000,
};
mag160c_temp_tables_t t;
memset(&t, 0, sizeof(t));
t.thresholds = thresh;
t.pwl = pwl;
t.pixel_count = 2;
t.band_count = 2;
t.baseline = baseline;
t.has_baseline = 1;
uint16_t out[2];
mag160c_temp_calibrate(frame, &t, out);
assert(out[0] == 2062); /* 2000 + (1000*256>>12) */
assert(out[1] == 3016); /* 2000 + (16256*256>>12) */
/* negative diff: (int16)(100-200)>>1 = -50 -> 1000 + (-50*4096>>12) = 950 */
const uint16_t f2[1] = {100};
const uint16_t b2[1] = {200};
mag160c_temp_tables_t t2;
memset(&t2, 0, sizeof(t2));
t2.thresholds = thresh;
t2.pwl = pwl;
t2.pixel_count = 1;
t2.band_count = 2;
t2.baseline = b2;
t2.has_baseline = 1;
uint16_t out2[1];
mag160c_temp_calibrate(f2, &t2, out2);
assert(out2[0] == 950);
}
static void test_convert_response(void) {
const uint16_t frame[4] = {100, 200, 300, 400};
const uint16_t baseline[4] = {0, 0, 0, 0};
mag160c_temp_gain_t cfg;
memset(&cfg, 0, sizeof(cfg));
cfg.gain = 0;
cfg.shift = 0;
cfg.coeff = 4;
cfg.baseline = baseline;
uint16_t out[4];
uint32_t sat = mag160c_temp_convert_response(frame, 4, &cfg, out);
/* base = (50000-0)>>0 = 50000 (< 0xffff, not clamped); v = 50000 + diff*4 */
assert(sat == 0);
assert(out[0] == 50400 && out[1] == 50800 && out[2] == 51200 && out[3] == 51600);
mag160c_temp_gain_t cfg2;
memset(&cfg2, 0, sizeof(cfg2));
cfg2.gain = 49900; /* base = (50000-49900)>>0 = 100 */
cfg2.shift = 0;
cfg2.coeff = 1;
cfg2.baseline = NULL;
sat = mag160c_temp_convert_response(frame, 4, &cfg2, out);
assert(sat == 0);
assert(out[0] == 200 && out[1] == 300 && out[2] == 400 && out[3] == 500);
}
static void test_t2e(void) {
/* index 0 = value where (v+0xc350)>>13 == 0 -> v = -0xc350 */
assert(mag160c_temp_t2e_interp(-0xc350) == 1000);
/* at -0xc350 + 8192, idx=1 -> 0x4d3 = 1235 */
assert(mag160c_temp_t2e_interp(-0xc350 + 8192) == 0x4d3);
/* monotonic curve sanity (avoid int32 overflow; vendor math wraps) */
const int32_t a = mag160c_temp_t2e_interp(-0xc350);
const int32_t b = mag160c_temp_t2e_interp(0x40000);
assert(a < b);
}
int main(void) {
test_calibrate_band_select();
test_calibrate_interp_clamp();
test_convert_response();
test_t2e();
printf("test_temp: all passed\n");
return 0;
}