diff --git a/Makefile.am b/Makefile.am index 88688d98..7fb7c61a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -26,6 +26,7 @@ EXTRA_DIST = \ test/run-parse.sh \ test/fixtures/manifest.txt \ test/fixtures/shearwater_petrel2-0001.bin \ + test/fixtures/shearwater_perdix2-0001.bin \ test/fixtures/hw_ostc5-0001.bin \ test/fixtures/garmin_descent_mk1-0001.bin \ test/test_descriptor_product_name.c diff --git a/src/Makefile.am b/src/Makefile.am index 67230fff..99a9b39c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -59,7 +59,7 @@ libdivecomputer_la_SOURCES = \ zeagle_n2ition3.h zeagle_n2ition3.c \ atomics_cobalt.h atomics_cobalt.c atomics_cobalt_parser.c \ shearwater_common.h shearwater_common.c \ - shearwater_predator.h shearwater_predator.c shearwater_predator_parser.c \ + shearwater_predator.h shearwater_predator.c shearwater_predator_parser.c shearwater_tank_gas.h \ shearwater_petrel.h shearwater_petrel.c \ diverite_nitekq.h diverite_nitekq.c diverite_nitekq_parser.c \ citizen_aqualand.h citizen_aqualand.c citizen_aqualand_parser.c \ diff --git a/src/shearwater_predator_parser.c b/src/shearwater_predator_parser.c index ff1174d8..9e52926e 100644 --- a/src/shearwater_predator_parser.c +++ b/src/shearwater_predator_parser.c @@ -32,6 +32,7 @@ #include "platform.h" #include "array.h" #include "field-cache.h" +#include "shearwater_tank_gas.h" #define ISINSTANCE(parser) ( \ dc_parser_isinstance((parser), &shearwater_predator_parser_vtable) || \ @@ -147,6 +148,7 @@ typedef struct shearwater_predator_tank_t { char name[2]; unsigned int battery; dc_tank_usage_t usage; + unsigned int gasmix; } shearwater_predator_tank_t; struct shearwater_predator_parser_t { @@ -320,6 +322,7 @@ shearwater_common_parser_create (dc_parser_t **out, dc_context_t *context, const memset (parser->tank[i].name, 0, sizeof (parser->tank[i].name)); parser->tank[i].battery = 0; parser->tank[i].usage = DC_TANK_USAGE_NONE; + parser->tank[i].gasmix = UNDEFINED; parser->tankidx[i] = i; } parser->aimode = AI_OFF; @@ -570,6 +573,21 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) unsigned int sub_mode = SM_OC_REC; unsigned int stack_time_total_s = 0; unsigned int stack_time_remaining_s; + + // AI-generated (Claude) + // The gas mix in use and the tank pressures of every sample, used to work + // out which gas mix each tank is on once all samples have been seen. + unsigned int maxsamples = length > offset ? (length - offset) / parser->samplesize : 0; + unsigned int *trace_gas = (unsigned int *) calloc (maxsamples + 1, sizeof (unsigned int)); + unsigned int *trace_pressure = (unsigned int *) calloc ((maxsamples + 1) * NTANKS, sizeof (unsigned int)); + if (trace_gas == NULL || trace_pressure == NULL) { + ERROR (abstract->context, "Failed to allocate memory."); + free (trace_gas); + free (trace_pressure); + return DC_STATUS_NOMEMORY; + } + unsigned int gas_current = SHEARWATER_TANK_GAS_NONE; + while (offset + parser->samplesize <= length) { // Ignore empty samples. if (array_isequal (data + offset, parser->samplesize, 0x00)) { @@ -613,6 +631,8 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) if (idx >= ngasmixes) { if (idx >= NGASMIXES) { ERROR (abstract->context, "Maximum number of gas mixes reached."); + free (trace_gas); + free (trace_pressure); return DC_STATUS_NOMEMORY; } gasmix[idx].oxygen = o2; @@ -622,11 +642,13 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) } gasmix[idx].active = 1; + gas_current = idx; o2_previous = o2; he_previous = he; dil_previous = ccr; } + trace_gas[nsamples - 1] = gas_current; // Tank pressure if (logversion >= 7) { @@ -654,6 +676,7 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) } tank[id].endpressure = pressure; tank[id].battery |= battery; + trace_pressure[(nsamples - 1) * NTANKS + id] = pressure; } } } @@ -673,6 +696,8 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) tank[id].endpressure = pressure; } tank[id].endpressure = pressure; + if (nsamples) + trace_pressure[(nsamples - 1) * NTANKS + id] = pressure; } } } @@ -838,6 +863,8 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) for (unsigned int i = 0; i <= 4; ++i) { if (parser->opening[i] == UNDEFINED || parser->closing[i] == UNDEFINED) { ERROR (abstract->context, "Opening or closing record %u not found.", i); + free (trace_gas); + free (trace_pressure); return DC_STATUS_DATAFORMAT; } } @@ -916,6 +943,9 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) parser->headersize = headersize; parser->footersize = footersize; parser->ngasmixes = 0; + unsigned int gasmap[NGASMIXES]; + for (unsigned int i = 0; i < NGASMIXES; ++i) + gasmap[i] = UNDEFINED; if (divemode != M_FREEDIVE) { for (unsigned int i = 0; i < ngasmixes; ++i) { if (gasmix[i].oxygen == 0 && gasmix[i].helium == 0) @@ -924,10 +954,39 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) continue; if (gasmix[i].diluent && !shearwater_predator_is_ccr (divemode)) continue; + gasmap[i] = parser->ngasmixes; parser->gasmix[parser->ngasmixes] = gasmix[i]; parser->ngasmixes++; } } + + // AI-generated (Claude) + // For open circuit dives, the begin pressure is the highest reading, not + // the first one: a stage with its valve closed until the gas switch first + // reads the pressure left in its regulator. And link each tank to the gas + // mix it was used on, if that is clear from the pressure drops. + unsigned int assigned[NTANKS]; + unsigned int sidemount = 0; + unsigned int oc = divemode == M_OC_TEC || divemode == M_OC_REC || divemode == M_AVELO; + for (unsigned int i = 0; i < NTANKS; ++i) { + assigned[i] = SHEARWATER_TANK_GAS_NONE; + if (tank[i].usage == DC_TANK_USAGE_SIDEMOUNT) + sidemount |= 1u << i; + for (unsigned int n = 0; n < nsamples && oc; ++n) { + if (trace_pressure[n * NTANKS + i] > tank[i].beginpressure) + tank[i].beginpressure = trace_pressure[n * NTANKS + i]; + } + } + if (oc) { + // At least 10 bar, in units of 2 psi, rounded up. + shearwater_tank_gas_assign (trace_gas, trace_pressure, nsamples, NTANKS, ngasmixes, + sidemount, 73, assigned); + } + for (unsigned int i = 0; i < NTANKS; ++i) + tank[i].gasmix = assigned[i] < NGASMIXES ? gasmap[assigned[i]] : UNDEFINED; + free (trace_gas); + free (trace_pressure); + parser->ntanks = 0; for (unsigned int i = 0; i < NTANKS; ++i) { if (tank[i].active) { @@ -1090,7 +1149,7 @@ shearwater_predator_parser_get_field (dc_parser_t *abstract, dc_field_type_t typ tank->workpressure = 0.0; tank->beginpressure = parser->tank[flags].beginpressure * 2 * PSI / BAR; tank->endpressure = parser->tank[flags].endpressure * 2 * PSI / BAR; - tank->gasmix = DC_GASMIX_UNKNOWN; + tank->gasmix = parser->tank[flags].gasmix == UNDEFINED ? DC_GASMIX_UNKNOWN : parser->tank[flags].gasmix; tank->usage = parser->tank[flags].usage; break; case DC_FIELD_SALINITY: diff --git a/src/shearwater_tank_gas.h b/src/shearwater_tank_gas.h new file mode 100644 index 00000000..b3fd5cb8 --- /dev/null +++ b/src/shearwater_tank_gas.h @@ -0,0 +1,123 @@ +/* + * libdivecomputer + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +// AI-generated (Claude) + +/* Internal matching of Shearwater air integration transmitters to gas mixes. */ +#ifndef SHEARWATER_TANK_GAS_H +#define SHEARWATER_TANK_GAS_H + +#define SHEARWATER_TANK_GAS_NONE 0xFFFFFFFF +#define SHEARWATER_TANK_GAS_MAXTANKS 8 +#define SHEARWATER_TANK_GAS_MAXGASES 20 + +/* + * The logs record the gas mix in use and the transmitter pressures, but not + * which gas mix each transmitter is on. Assign each transmitter the gas mix + * it was clearly used most on: the pressure drop while that mix was in use. + * + * gas[n] is the gas mix in use at sample n (or SHEARWATER_TANK_GAS_NONE), and + * pressure[n * ntanks + i] is the reading of transmitter i at sample n (0 if + * there is none). Readings before a transmitter's highest reading are ignored, + * so the regulator of a closed stage bleeding down doesn't count as use. The + * transmitters in the sidemount mask are counted together and get the same + * gas mix. A transmitter gets a gas mix only if it used at least minuse, and + * at least twice as much as on any other mix. Otherwise result[i] is + * SHEARWATER_TANK_GAS_NONE. + */ +static void +shearwater_tank_gas_assign (const unsigned int gas[], const unsigned int pressure[], + unsigned int nsamples, unsigned int ntanks, unsigned int ngases, + unsigned int sidemount, int minuse, unsigned int result[]) +{ + int use[SHEARWATER_TANK_GAS_MAXTANKS][SHEARWATER_TANK_GAS_MAXGASES] = {{0}}; + const unsigned int stride = ntanks; + + if (ntanks > SHEARWATER_TANK_GAS_MAXTANKS) + ntanks = SHEARWATER_TANK_GAS_MAXTANKS; + if (ngases > SHEARWATER_TANK_GAS_MAXGASES) + ngases = SHEARWATER_TANK_GAS_MAXGASES; + + for (unsigned int i = 0; i < ntanks; ++i) { + unsigned int highest = 0, start = 0; + for (unsigned int n = 0; n < nsamples; ++n) { + if (pressure[n * stride + i] > highest) { + highest = pressure[n * stride + i]; + start = n; + } + } + if (!highest) + continue; + + // A drop between two readings only counts if the same gas mix was + // in use throughout, including the samples without a reading. + unsigned int previous = highest, previous_gas = gas[start], changed = 0; + for (unsigned int n = start + 1; n < nsamples; ++n) { + if (gas[n] != previous_gas) + changed = 1; + unsigned int p = pressure[n * stride + i]; + if (p == 0) + continue; + if (!changed && gas[n] < ngases) + use[i][gas[n]] += (int) previous - (int) p; + previous = p; + previous_gas = gas[n]; + changed = 0; + } + } + + // Sidemount tanks are breathed alternately on one gas mix. + unsigned int count = 0; + int pooled[SHEARWATER_TANK_GAS_MAXGASES] = {0}; + for (unsigned int i = 0; i < ntanks; ++i) { + if (sidemount & (1u << i)) { + count++; + for (unsigned int g = 0; g < ngases; ++g) + pooled[g] += use[i][g]; + } + } + if (count >= 2) { + for (unsigned int i = 0; i < ntanks; ++i) { + if (sidemount & (1u << i)) { + for (unsigned int g = 0; g < ngases; ++g) + use[i][g] = pooled[g]; + } + } + } + + for (unsigned int i = 0; i < ntanks; ++i) { + unsigned int best = 0; + int second = 0; + for (unsigned int g = 1; g < ngases; ++g) { + if (use[i][g] > use[i][best]) { + if (use[i][best] > second) + second = use[i][best]; + best = g; + } else if (use[i][g] > second) { + second = use[i][g]; + } + } + if (ngases && use[i][best] >= minuse && use[i][best] >= 2 * second) + result[i] = best; + else + result[i] = SHEARWATER_TANK_GAS_NONE; + } +} + +#endif diff --git a/test/Makefile.am b/test/Makefile.am index 6d07e758..3a1e35e4 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -3,17 +3,21 @@ AUTOMAKE_OPTIONS = subdir-objects AM_CPPFLAGS = -I$(top_builddir)/include -I$(top_srcdir)/include LDADD = $(top_builddir)/src/libdivecomputer.la -check_PROGRAMS = test_descriptor_product_name test_mares_iconhd_freedive +check_PROGRAMS = test_descriptor_product_name test_mares_iconhd_freedive test_shearwater_tank_gas test_shearwater_perdix2_tanks test_descriptor_product_name_SOURCES = test_descriptor_product_name.c test_mares_iconhd_freedive_SOURCES = test_mares_iconhd_freedive.c $(top_srcdir)/src/array.c test_mares_iconhd_freedive_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/src +test_shearwater_tank_gas_SOURCES = test_shearwater_tank_gas.c +test_shearwater_tank_gas_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/src +test_shearwater_perdix2_tanks_SOURCES = test_shearwater_perdix2_tanks.c -TESTS = test_descriptor_product_name test_mares_iconhd_freedive +TESTS = test_descriptor_product_name test_mares_iconhd_freedive test_shearwater_tank_gas test_shearwater_perdix2_tanks EXTRA_DIST = \ run-parse.sh \ fixtures/manifest.txt \ fixtures/shearwater_petrel2-0001.bin \ + fixtures/shearwater_perdix2-0001.bin \ fixtures/hw_ostc5-0001.bin \ fixtures/garmin_descent_mk1-0001.bin diff --git a/test/fixtures/manifest.txt b/test/fixtures/manifest.txt index 0ba8efd9..6696aedb 100644 --- a/test/fixtures/manifest.txt +++ b/test/fixtures/manifest.txt @@ -4,5 +4,6 @@ # All dives are the owner's own recordings, cleared for redistribution under # the library licence. No GPS or personal location data is present. shearwater_petrel2-0001.bin|Shearwater|Petrel 2 +shearwater_perdix2-0001.bin|Shearwater|Perdix 2 hw_ostc5-0001.bin|Heinrichs Weikamp|OSTC 5 garmin_descent_mk1-0001.bin|Garmin|Descentâ„¢ Mk1 diff --git a/test/fixtures/shearwater_perdix2-0001.bin b/test/fixtures/shearwater_perdix2-0001.bin new file mode 100644 index 00000000..9d179401 Binary files /dev/null and b/test/fixtures/shearwater_perdix2-0001.bin differ diff --git a/test/test_shearwater_perdix2_tanks.c b/test/test_shearwater_perdix2_tanks.c new file mode 100644 index 00000000..6503ac2a --- /dev/null +++ b/test/test_shearwater_perdix2_tanks.c @@ -0,0 +1,87 @@ +// AI-generated (Claude) +// Parses the Perdix 2 fixture, a dive with a sidemount pair on 21% (T1, T2) +// and stages on 50% (T3) and oxygen (T4), with the stage valves closed until +// the gas switch, and checks the tanks the parser reports. +#include +#include +#include + +#include +#include +#include + +#define NTANKS 4 + +int +main (void) +{ + // The gas mixes are 99%, 50% and 21%; diluent slot 5 is filtered out. + const unsigned int expected_gasmix[NTANKS] = {2, 2, 1, 0}; + // The highest readings, in bar. T3 and T4 first read 179 and 115 bar. + const double expected_begin[NTANKS] = {220.0, 232.0, 207.0, 162.0}; + int failures = 0; + + const char *srcdir = getenv ("srcdir"); + char path[1024]; + snprintf (path, sizeof (path), "%s/fixtures/shearwater_perdix2-0001.bin", srcdir ? srcdir : "."); + FILE *fp = fopen (path, "rb"); + if (fp == NULL) { + fprintf (stderr, "FAIL: cannot open %s\n", path); + return 1; + } + static unsigned char data[64 * 1024]; + size_t size = fread (data, 1, sizeof (data), fp); + fclose (fp); + + dc_context_t *context = NULL; + dc_context_new (&context); + dc_iterator_t *iterator = NULL; + dc_descriptor_t *descriptor = NULL, *d = NULL; + dc_descriptor_iterator_new (&iterator, context); + while (dc_iterator_next (iterator, &d) == DC_STATUS_SUCCESS) { + if (strcmp (dc_descriptor_get_vendor (d), "Shearwater") == 0 && + strcmp (dc_descriptor_get_product (d), "Perdix 2") == 0) { + descriptor = d; + break; + } + dc_descriptor_free (d); + } + dc_iterator_free (iterator); + if (descriptor == NULL) { + fprintf (stderr, "FAIL: no Perdix 2 descriptor\n"); + return 1; + } + + dc_parser_t *parser = NULL; + if (dc_parser_new2 (&parser, context, descriptor, data, size) != DC_STATUS_SUCCESS) { + fprintf (stderr, "FAIL: cannot create the parser\n"); + return 1; + } + + unsigned int ngasmixes = 0, ntanks = 0; + dc_parser_get_field (parser, DC_FIELD_GASMIX_COUNT, 0, &ngasmixes); + dc_parser_get_field (parser, DC_FIELD_TANK_COUNT, 0, &ntanks); + if (ngasmixes != 3 || ntanks != NTANKS) { + fprintf (stderr, "FAIL: %u gas mixes and %u tanks, expected 3 and %u\n", ngasmixes, ntanks, NTANKS); + failures++; + } else { + for (unsigned int i = 0; i < NTANKS; ++i) { + dc_tank_t tank = {0}; + dc_parser_get_field (parser, DC_FIELD_TANK, i, &tank); + if (tank.gasmix != expected_gasmix[i]) { + fprintf (stderr, "FAIL: tank %u on gas mix %d, expected %u\n", i, (int) tank.gasmix, expected_gasmix[i]); + failures++; + } + if (tank.beginpressure < expected_begin[i] - 1.0 || tank.beginpressure > expected_begin[i] + 1.0) { + fprintf (stderr, "FAIL: tank %u begin pressure %.1f bar, expected %.0f bar\n", i, tank.beginpressure, expected_begin[i]); + failures++; + } + } + } + + dc_parser_destroy (parser); + dc_descriptor_free (descriptor); + dc_context_free (context); + + return failures ? 1 : 0; +} diff --git a/test/test_shearwater_tank_gas.c b/test/test_shearwater_tank_gas.c new file mode 100644 index 00000000..78fd0a02 --- /dev/null +++ b/test/test_shearwater_tank_gas.c @@ -0,0 +1,142 @@ +// AI-generated (Claude) +#include + +#include "shearwater_tank_gas.h" + +#define NONE SHEARWATER_TANK_GAS_NONE +#define NTANKS 4 +#define NSAMPLES 60 +#define MINUSE 70 + +typedef struct { + unsigned int gas[NSAMPLES]; + unsigned int pressure[NSAMPLES * NTANKS]; +} trace_t; + +// Breathe gas mix 'gas' from sample 'from' up to (not including) sample 'to'. +static void +breathe (trace_t *t, unsigned int from, unsigned int to, unsigned int gas) +{ + for (unsigned int n = from; n < to; ++n) + t->gas[n] = gas; +} + +// Tank 'tank' reads 'start' at sample 'from', falling by 'step' per sample. +static void +reading (trace_t *t, unsigned int tank, unsigned int from, unsigned int to, unsigned int start, int step) +{ + for (unsigned int n = from; n < to; ++n) + t->pressure[n * NTANKS + tank] = start - step * (int) (n - from); +} + +static int +check (const char *name, const trace_t *t, unsigned int sidemount, const unsigned int expected[NTANKS]) +{ + unsigned int result[NTANKS]; + shearwater_tank_gas_assign (t->gas, t->pressure, NSAMPLES, NTANKS, 3, sidemount, MINUSE, result); + for (unsigned int i = 0; i < NTANKS; ++i) { + if (result[i] != expected[i]) { + fprintf (stderr, "FAIL: %s: tank %u got %d, expected %d\n", name, i, (int) result[i], (int) expected[i]); + return 1; + } + } + return 0; +} + +int +main (void) +{ + int failures = 0; + + // Sidemount pair on bottom gas (2), stages on 50% (1) and oxygen (0) + // with their valves closed until the switch: before that, the stage + // transmitters read the pressure in their regulators bleeding down. + trace_t a = {0}; + breathe (&a, 0, 30, 2); + breathe (&a, 30, 45, 1); + breathe (&a, 45, 60, 0); + reading (&a, 0, 0, 30, 1500, 15); + reading (&a, 0, 30, 60, 1065, 0); + reading (&a, 1, 0, 30, 1550, 15); + reading (&a, 1, 30, 60, 1115, 0); + reading (&a, 2, 0, 30, 1000, 20); + reading (&a, 2, 30, 45, 1400, 15); + reading (&a, 2, 45, 60, 1190, 0); + reading (&a, 3, 0, 45, 900, 15); + reading (&a, 3, 45, 60, 1100, 15); + const unsigned int expected_a[NTANKS] = {2, 2, 1, 0}; + failures += check ("sidemount with closed stages", &a, 0x3, expected_a); + + // Without the sidemount flag, each tank still gets the gas it was used on. + failures += check ("doubles without sidemount flag", &a, 0, expected_a); + + // Cave staging: sidemount pair and two staged tanks, all on air (0). + trace_t b = {0}; + breathe (&b, 0, 60, 0); + reading (&b, 0, 0, 20, 1500, 10); + reading (&b, 0, 20, 60, 1310, 0); + reading (&b, 1, 0, 20, 1500, 0); + reading (&b, 1, 20, 60, 1500, 10); + reading (&b, 2, 0, 40, 1400, 0); + reading (&b, 2, 40, 60, 1400, 10); + reading (&b, 3, 0, 60, 1400, 3); + const unsigned int expected_b[NTANKS] = {0, 0, 0, 0}; + failures += check ("all tanks on one gas", &b, 0x3, expected_b); + + // A stage that was carried but never breathed, and a tank without data. + trace_t c = {0}; + breathe (&c, 0, 40, 2); + breathe (&c, 40, 60, 1); + reading (&c, 0, 0, 60, 1500, 10); + reading (&c, 1, 0, 60, 1400, 0); + const unsigned int expected_c[NTANKS] = {2, NONE, NONE, NONE}; + failures += check ("unused stage", &c, 0, expected_c); + + // Used about as much on two gas mixes (a switch logged late): no guess. + trace_t d = {0}; + breathe (&d, 0, 30, 1); + breathe (&d, 30, 60, 0); + reading (&d, 0, 0, 60, 1200, 5); + const unsigned int expected_d[NTANKS] = {NONE, NONE, NONE, NONE}; + failures += check ("ambiguous", &d, 0, expected_d); + + // One side of a sidemount pair barely breathed: it goes with its pair. + trace_t e = {0}; + breathe (&e, 0, 60, 2); + reading (&e, 0, 0, 60, 1500, 15); + reading (&e, 1, 0, 60, 1500, 0); + reading (&e, 1, 50, 60, 1500, 1); + const unsigned int expected_e[NTANKS] = {2, 2, NONE, NONE}; + failures += check ("one-sided sidemount", &e, 0x3, expected_e); + failures += check ("one-sided without sidemount flag", &e, 0, (const unsigned int[NTANKS]) {2, NONE, NONE, NONE}); + + // The limits are inclusive: exactly the minimum use, and exactly twice + // the use on the next mix, still get a link; one less doesn't. + trace_t f = {0}; + breathe (&f, 0, 30, 1); + breathe (&f, 30, 60, 0); + reading (&f, 0, 0, 30, 1000, 0); + reading (&f, 0, 30, 31, 1000, 0); + reading (&f, 0, 31, 60, 930, 0); + reading (&f, 1, 0, 2, 1000, 35); + reading (&f, 1, 2, 30, 965, 0); + reading (&f, 1, 30, 31, 965, 0); + reading (&f, 1, 31, 60, 895, 0); + reading (&f, 2, 0, 30, 1000, 0); + reading (&f, 2, 30, 31, 1000, 0); + reading (&f, 2, 31, 60, 931, 0); + const unsigned int expected_f[NTANKS] = {0, 0, NONE, NONE}; + failures += check ("inclusive limits", &f, 0, expected_f); + + // A drop across missing readings, during which the gas mix changed and + // changed back, can't be attributed to either mix. + trace_t g = {0}; + breathe (&g, 0, 60, 0); + breathe (&g, 20, 40, 1); + reading (&g, 0, 0, 10, 1500, 0); + reading (&g, 0, 50, 60, 1200, 0); + const unsigned int expected_g[NTANKS] = {NONE, NONE, NONE, NONE}; + failures += check ("gas change during missing readings", &g, 0, expected_g); + + return failures ? 1 : 0; +}