Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
c_abi_smoke.c
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 https://github.com/lrleon/Aleph-w
6
7 This file is part of Aleph-w library
8
9 Copyright (c) 2002-2026 Leandro Rabindranath Leon
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28*/
29
30/*
31 * c_abi_smoke.c — Phase 25: smoke test for the flat C ABI.
32 *
33 * Compiled as C99 (no C++), linked against libalephca_c. Drives a Game of
34 * Life blinker through the C API and checks it oscillates with period 2,
35 * proving the ABI is usable from a pure-C consumer.
36 */
37
38#include <stdint.h>
39#include <stdio.h>
40#include <stdlib.h>
41
42#include "ca-c-api.h"
43
44#define CHECK(cond, msg) \
45 do { \
46 if (!(cond)) { \
47 fprintf(stderr, "FAIL: %s (line %d): %s\n", msg, __LINE__, \
48 aleph_ca_error_message_v1()); \
49 return 1; \
50 } \
51 } while (0)
52
53static int cell(const aleph_ca_engine_t *e, size_t r, size_t c)
54{
55 int32_t v = -1;
56 int rc = aleph_ca_get_cell_v1(e, r, c, &v);
57 if (rc != ALEPH_CA_OK)
58 return -1;
59 return (int) v;
60}
61
62int main(void)
63{
64 /* Version check. */
65 uint32_t ver = aleph_ca_api_version_v1();
66 CHECK((ver >> 16) == 1u, "major version is 1");
67
68 /* 5x5 toroidal Game of Life. */
70 CHECK(e != NULL, "engine created");
71
72 size_t rows = 0, cols = 0;
73 CHECK(aleph_ca_extents_v1(e, &rows, &cols) == ALEPH_CA_OK, "extents");
74 CHECK(rows == 5 && cols == 5, "extents are 5x5");
75
76 /* Seed a horizontal blinker on row 2. */
77 CHECK(aleph_ca_set_cell_v1(e, 2, 1, 1) == ALEPH_CA_OK, "set (2,1)");
78 CHECK(aleph_ca_set_cell_v1(e, 2, 2, 1) == ALEPH_CA_OK, "set (2,2)");
79 CHECK(aleph_ca_set_cell_v1(e, 2, 3, 1) == ALEPH_CA_OK, "set (2,3)");
80
81 /* One step: the blinker must become vertical on column 2. */
82 CHECK(aleph_ca_step_v1(e) == ALEPH_CA_OK, "step 1");
83 CHECK(aleph_ca_steps_run_v1(e) == 1, "one step run");
84 CHECK(cell(e, 1, 2) == 1 && cell(e, 2, 2) == 1 && cell(e, 3, 2) == 1,
85 "vertical blinker after 1 step");
86 CHECK(cell(e, 2, 1) == 0 && cell(e, 2, 3) == 0,
87 "horizontal arms cleared after 1 step");
88
89 /* Another step: back to horizontal (period 2). */
90 CHECK(aleph_ca_step_v1(e) == ALEPH_CA_OK, "step 2");
91 CHECK(cell(e, 2, 1) == 1 && cell(e, 2, 2) == 1 && cell(e, 2, 3) == 1,
92 "horizontal blinker after 2 steps");
93
94 /* copy_frame round-trip: exactly three live cells. */
95 int32_t buf[25];
96 CHECK(aleph_ca_copy_frame_v1(e, buf, 25) == ALEPH_CA_OK, "copy_frame");
97 int live = 0;
98 for (int i = 0; i < 25; ++i)
99 live += (buf[i] != 0);
100 CHECK(live == 3, "three live cells in frame");
101
102 /* Error path: a too-small buffer must be rejected, not crash. */
104 "small buffer rejected");
105 CHECK(aleph_ca_last_error_v1() == ALEPH_CA_ERR_RANGE, "last_error set");
106
107 /* Error path: out-of-range coordinate. */
109 "out-of-range set rejected");
110
112 aleph_ca_destroy_engine_v1(NULL); /* must be a no-op */
113
114 printf("c_abi_smoke: OK\n");
115 return 0;
116}
#define CHECK(cond, msg)
Definition c_abi_smoke.c:44
static int cell(const aleph_ca_engine_t *e, size_t r, size_t c)
Definition c_abi_smoke.c:53
int main(void)
Definition c_abi_smoke.c:62
aleph_ca_engine_t * aleph_ca_create_gol_engine_v1(size_t rows, size_t cols)
Definition ca-c-api.cc:108
int aleph_ca_copy_frame_v1(const aleph_ca_engine_t *engine, int32_t *out, size_t out_len)
Definition ca-c-api.cc:247
void aleph_ca_destroy_engine_v1(aleph_ca_engine_t *engine)
Definition ca-c-api.cc:138
int aleph_ca_extents_v1(const aleph_ca_engine_t *engine, size_t *rows, size_t *cols)
Definition ca-c-api.cc:143
int aleph_ca_step_v1(aleph_ca_engine_t *engine)
Definition ca-c-api.cc:199
int aleph_ca_set_cell_v1(aleph_ca_engine_t *engine, size_t row, size_t col, int32_t value)
Definition ca-c-api.cc:157
int aleph_ca_last_error_v1(void)
Definition ca-c-api.cc:98
int aleph_ca_get_cell_v1(const aleph_ca_engine_t *engine, size_t row, size_t col, int32_t *out)
Definition ca-c-api.cc:178
size_t aleph_ca_steps_run_v1(const aleph_ca_engine_t *engine)
Definition ca-c-api.cc:272
uint32_t aleph_ca_api_version_v1(void)
Definition ca-c-api.cc:92
size_t * rows
Definition ca-c-api.h:112
@ ALEPH_CA_OK
Definition ca-c-api.h:74
@ ALEPH_CA_ERR_RANGE
Definition ca-c-api.h:76
size_t cols
Definition ca-c-api.h:105
gsl_rng * r