diff --git a/src/c/departures_window.c b/src/c/departures_window.c index 35f0e62..4c61771 100644 --- a/src/c/departures_window.c +++ b/src/c/departures_window.c @@ -55,19 +55,6 @@ static void rebuild(Instance *inst) { inst->count = model_departures(instance_mode(inst), inst->refs, MAX_DEPARTURES); } -static uint8_t shown_stops(Instance *inst) { - uint16_t seen = 0; - uint8_t n = 0; - for (uint8_t i = 0; i < inst->count; i++) { - uint16_t bit = (uint16_t)(1u << inst->refs[i].stop); - if (!(seen & bit)) { - seen |= bit; - n++; - } - } - return n; -} - static void sync_loading(Instance *inst) { bool loading = inst->window && inst->count == 0 && model_state() == STATE_LOADING; if (loading == inst->was_loading) { @@ -104,7 +91,7 @@ static void draw_header(GContext *ctx, const Layer *cell, uint16_t section, void char buf[24]; snprintf(buf, sizeof(buf), "%s ยท %u", instance_mode(inst) == DEPARTURES_FAVORITES ? "FAVORITES" : "NEARBY", - shown_stops(inst)); + model_distinct_stops(inst->refs, inst->count)); graphics_context_set_text_color(ctx, GColorLightGray); graphics_draw_text(ctx, buf, fonts_get_system_font(FONT_KEY_GOTHIC_14_BOLD), GRect(4, 0, bounds.size.w - 8 - CLOCK_W, bounds.size.h), diff --git a/src/c/model.c b/src/c/model.c index def46fe..0a31f20 100644 --- a/src/c/model.c +++ b/src/c/model.c @@ -185,6 +185,19 @@ uint8_t model_departures(DeparturesMode mode, DepartureRef *out, uint8_t max) { return n; } +uint8_t model_distinct_stops(const DepartureRef *refs, uint8_t count) { + uint32_t seen = 0; + uint8_t n = 0; + for (uint8_t i = 0; i < count; i++) { + uint32_t bit = 1u << refs[i].stop; + if (!(seen & bit)) { + seen |= bit; + n++; + } + } + return n; +} + LineDetail *model_line_detail(void) { return &s_line_detail; } diff --git a/src/c/model.h b/src/c/model.h index b0797ce..0d6f5cd 100644 --- a/src/c/model.h +++ b/src/c/model.h @@ -2,7 +2,7 @@ #include -#define MAX_STOPS 16 +#define MAX_STOPS 32 #define MAX_ROWS 12 #define NAME_LEN 32 #define ID_LEN 8 @@ -102,6 +102,7 @@ void model_set_config(bool vibrate, uint8_t lang); bool model_row_visible(const Row *row); bool model_row_info(const Row *row); uint8_t model_departures(DeparturesMode mode, DepartureRef *out, uint8_t max); +uint8_t model_distinct_stops(const DepartureRef *refs, uint8_t count); LineDetail *model_line_detail(void); void model_line_begin(void); bool model_line_complete(void); diff --git a/src/pkjs/wire.js b/src/pkjs/wire.js index 905866c..6f86662 100644 --- a/src/pkjs/wire.js +++ b/src/pkjs/wire.js @@ -1,6 +1,6 @@ var KINDS = { tram: 0, metro: 1, bus: 2, train: 3, bike: 4 }; var LANGS = { en: 0, fr: 1, nl: 2 }; -var MAX_STOPS = 16; +var MAX_STOPS = 32; var MAX_ROWS = 12; function gcolor8(hex) { diff --git a/tests/c/test_model.c b/tests/c/test_model.c index b2c9c74..f3bffde 100644 --- a/tests/c/test_model.c +++ b/tests/c/test_model.c @@ -427,6 +427,54 @@ static void test_stress_full_board_both_modes(void) { } } +static void test_distinct_stops_counts_above_sixteen(void) { + DepartureRef refs[6] = { + { .stop = 0, .row = 0 }, + { .stop = 0, .row = 1 }, + { .stop = 15, .row = 0 }, + { .stop = 16, .row = 0 }, + { .stop = 31, .row = 0 }, + { .stop = 31, .row = 1 }, + }; + assert(model_distinct_stops(refs, 6) == 4); + assert(model_distinct_stops(refs, 0) == 0); +} + +static void test_thirty_stop_board_departures(void) { + model_begin(30); + assert(model_stop_count() == 30); + for (uint8_t i = 0; i < 26; i++) { + char id[ID_LEN]; + snprintf(id, sizeof(id), "f%u", i); + put_stop(i, id, "FAV", true, 1); + model_stop_at(i)->nearby = 0; + put_row(i, 0, "9", 0, 0, (int16_t)(i + 1)); + } + put_stop(26, "n1", "NEAR", false, 1); + put_row(26, 0, "62", 0, 0, 2); + put_stop(27, "n2", "NEAR2", false, 1); + put_row(27, 0, "71", 0, 0, 5); + put_stop(28, "sncb", "GARE", false, 1); + put_row(28, 0, "IC", 3, 0, 4); + put_stop(29, "villo", "VILLO", false, 1); + put_row(29, 0, "V", 4, 0, -1); + DepartureRef out[MAX_DEPARTURES]; + uint8_t n = model_departures(DEPARTURES_NEARBY, out, MAX_DEPARTURES); + assert(n == 4); + bool saw_train = false; + bool saw_villo = false; + for (uint8_t k = 0; k < n; k++) { + saw_train |= out[k].stop == 28; + saw_villo |= out[k].stop == 29; + } + assert(saw_train); + assert(saw_villo); + assert(model_distinct_stops(out, n) == 4); + n = model_departures(DEPARTURES_FAVORITES, out, MAX_DEPARTURES); + assert(n == 26); + assert(model_distinct_stops(out, n) == 26); +} + static void test_stale_flag(void) { model_note_data_arrival(1000); assert(!model_stale(1000)); @@ -486,6 +534,8 @@ int main(void) { test_departures_favorites_respects_visibility(); test_departures_full_board_bounds(); test_stress_full_board_both_modes(); + test_distinct_stops_counts_above_sixteen(); + test_thirty_stop_board_departures(); test_stale_flag(); test_staleness_pure_decisions(); test_check_staleness_stateful_cycle(); diff --git a/tests/c/test_msg.c b/tests/c/test_msg.c index a4f146c..d51af4b 100644 --- a/tests/c/test_msg.c +++ b/tests/c/test_msg.c @@ -220,6 +220,58 @@ static void test_int32_extremes(void) { } } +static void send_full_stop(uint8_t index, uint8_t count, const char *id, const char *name, + uint8_t fav, uint8_t nearby, uint8_t row_kind, int32_t min1) { + TestDict d = {0}; + td_int(&d, MESSAGE_KEY_KIND, 1); + td_int(&d, MESSAGE_KEY_STOP_INDEX, index); + td_int(&d, MESSAGE_KEY_STOP_COUNT, count); + td_str(&d, MESSAGE_KEY_STOP_ID, id); + td_str(&d, MESSAGE_KEY_STOP_NAME, name); + td_int(&d, MESSAGE_KEY_STOP_FAV, fav); + td_int(&d, MESSAGE_KEY_STOP_NEARBY, nearby); + td_int(&d, MESSAGE_KEY_STOP_ROWS, 1); + td_send(&d); + d.count = 0; + td_int(&d, MESSAGE_KEY_KIND, 2); + td_int(&d, MESSAGE_KEY_STOP_INDEX, index); + td_int(&d, MESSAGE_KEY_ROW_INDEX, 0); + td_str(&d, MESSAGE_KEY_ROW_LINE, row_kind == 3 ? "IC" : row_kind == 4 ? "V" : "62"); + td_int(&d, MESSAGE_KEY_ROW_KIND, row_kind); + td_int(&d, MESSAGE_KEY_ROW_MIN1, min1); + td_send(&d); +} + +static void test_thirty_stop_board_fills_end_to_end(void) { + for (uint8_t i = 0; i < 26; i++) { + char id[ID_LEN]; + snprintf(id, sizeof(id), "f%u", i); + send_full_stop(i, 30, id, "FAV", 1, 0, 0, (int32_t)(i + 1)); + } + send_full_stop(26, 30, "n1", "NEAR", 0, 1, 0, 2); + send_full_stop(27, 30, "n2", "NEAR2", 0, 1, 0, 5); + send_full_stop(28, 30, "sncb", "GARE", 0, 1, 3, 4); + send_full_stop(29, 30, "villo", "VILLO", 0, 1, 4, -1); + assert(model_stop_count() == 30); + assert(strcmp(model_stop_at(29)->id, "villo") == 0); + assert(model_stop_at(29)->rows[0].kind == 4); + DepartureRef out[MAX_DEPARTURES]; + uint8_t n = model_departures(DEPARTURES_NEARBY, out, MAX_DEPARTURES); + assert(n == 4); + bool saw_train = false; + bool saw_villo = false; + for (uint8_t k = 0; k < n; k++) { + saw_train |= out[k].stop == 28; + saw_villo |= out[k].stop == 29; + } + assert(saw_train); + assert(saw_villo); + assert(model_distinct_stops(out, n) == 4); + n = model_departures(DEPARTURES_FAVORITES, out, MAX_DEPARTURES); + assert(n == 26); + assert(model_distinct_stops(out, n) == 26); +} + static void test_done_message_states(void) { send_stop(0, 1, "6419", "BOTANIQUE", 1); int polls = g_polls; @@ -446,6 +498,7 @@ int main(void) { test_stop_nearby_fill(); test_string_termination(); test_int32_extremes(); + test_thirty_stop_board_fills_end_to_end(); test_done_message_states(); test_kind3_stamps_arrival_for_staleness(); test_error_message_states(); diff --git a/tests/pkjs.test.js b/tests/pkjs.test.js index f447301..2e5d2cb 100644 --- a/tests/pkjs.test.js +++ b/tests/pkjs.test.js @@ -201,7 +201,7 @@ test('buildLineUrl composes query with encoding', function () { test('boardToMessages caps stops and rows at MAX_STOPS and MAX_ROWS', function () { var stops = []; - for (var i = 0; i < 17; i++) { + for (var i = 0; i < 33; i++) { var rows = []; for (var j = 0; j < 13; j++) { rows.push({ @@ -224,8 +224,8 @@ test('boardToMessages caps stops and rows at MAX_STOPS and MAX_ROWS', function ( var msgs = wire.boardToMessages({ stale: false, stops: stops }, 'nl'); var kind1s = msgs.filter(function (m) { return m.KIND === 1; }); var kind2s = msgs.filter(function (m) { return m.KIND === 2; }); - assert.equal(kind1s.length, 16); - assert.equal(kind1s[0].STOP_COUNT, 16); + assert.equal(kind1s.length, 32); + assert.equal(kind1s[0].STOP_COUNT, 32); var bigStop = kind1s[0]; assert.equal(bigStop.STOP_ROWS, 12); var bigStopRows = kind2s.filter(function (m) { return m.STOP_INDEX === 0; }); @@ -592,8 +592,8 @@ test('boardToMessages caps an oversized board with oversized rows', function () }); } var msgs = wire.boardToMessages({ stale: false, stops: stops }, 'fr'); - assert.equal(msgs.filter(function (m) { return m.KIND === 1; }).length, 16); - assert.equal(msgs.filter(function (m) { return m.KIND === 2; }).length, 16 * 12); + assert.equal(msgs.filter(function (m) { return m.KIND === 1; }).length, 32); + assert.equal(msgs.filter(function (m) { return m.KIND === 2; }).length, 32 * 12); assert.equal(msgs[msgs.length - 1].KIND, 3); });