LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
test_lifecycle.c
浏览该文件的文档.
1/**
2 * Copyright (C) 2026 HJimmyK (Jericho Knox)
3 *
4 * This file is part of LMMP.
5 *
6 * LMMP is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <https://www.gnu.org/licenses/>.
19 *
20 * SPDX-License-Identifier: LGPL-3.0-or-later
21 */
22
23/*
24 * lmmp_global_init / lmmp_global_deinit 注册机制测试:
25 * 1. 生命周期幂等与重入(重复 init/deinit、释放后重建);
26 * 2. 全周期堆配平(计数分配器验证注册表释放了全部线程局部堆资源);
27 * 3. 线程隔离(主线程 init 后创建新线程再次 init/deinit,互不影响)。
28 */
29
30#include "lmmp/lmmp.h"
31#include "lmmp/lmmpn.h"
32#include "lmmp/numth.h"
33#include "cases.h"
34
35#include <stdatomic.h>
36#include <stdlib.h>
37#include <string.h>
38
39#if defined(LMMP_WINDOWS)
40#include <windows.h>
41#include <process.h>
42#else
43#include <pthread.h>
44#endif
45
46enum { BIG_N = 64, DECIMAL_CAP = 20 * BIG_N + 8, FACTORIAL_N = 65537 };
47
48static void fill_big(mp_ptr a) {
49 for (mp_size_t i = 0; i < BIG_N; ++i)
50 a[i] = 0x9e3779b97f4a7c15ull * (i + 1) + 0x123456789abcdefull;
51 a[BIG_N - 1] |= 1;
52}
53
55 return lmmp_to_str_(out, a, BIG_N, 10);
56}
57
59 mp_ptr buffer = calloc(n + 1, sizeof(*buffer));
60 if (!buffer)
61 abort();
62 return buffer;
63}
64
65TEST_CASE("global/lifecycle", lifecycle_idempotent) {
66 mp_limb_t a[BIG_N], dst[2 * BIG_N], dst_ref[2 * BIG_N];
67 mp_byte_t before[DECIMAL_CAP], after[DECIMAL_CAP];
68 fill_big(a);
69
72
73 mp_size_t len = big_to_decimal(before, a);
74 TEST_CHECK(len > BIG_N * 15);
75 lmmp_mul_(dst_ref, a, BIG_N, a, BIG_N);
76
77 mp_bitcnt_t bits = 0;
79 mp_ptr fac = factorial_buffer(rn);
80 TEST_CHECK(lmmp_factorial_(fac, bits, rn + 1, FACTORIAL_N) > 0);
81
85
86 TEST_CHECK(big_to_decimal(after, a) == len && memcmp(after, before, len) == 0);
87 lmmp_mul_(dst, a, BIG_N, a, BIG_N);
88 TEST_CHECK(memcmp(dst, dst_ref, sizeof(dst)) == 0);
89
90 mp_bitcnt_t bits2 = 0;
92 TEST_CHECK(rn2 == rn && bits2 == bits);
93 mp_ptr fac2 = factorial_buffer(rn2);
94 TEST_CHECK(lmmp_factorial_(fac2, bits2, rn2 + 1, FACTORIAL_N) > 0);
95 TEST_CHECK(rn == rn2 && memcmp(fac, fac2, (rn + 1) * sizeof(*fac)) == 0);
96 free(fac2);
97 free(fac);
98}
99
100static atomic_long live_allocations;
101
102static void *count_alloc(size_t n) {
103 void *p = malloc(n);
104 if (p)
106 return p;
107}
108
109static void count_free(void *p) {
110 if (p)
112 free(p);
113}
114
115static void *count_realloc(void *p, size_t n) {
116 return p ? realloc(p, n) : count_alloc(n);
117}
118
119TEST_CASE("global/lifecycle", heap_balance_full_cycle) {
121 const lmmp_heap_allocator_t libc = {malloc, free, realloc};
122 lmmp_set_heap_allocator(&counting);
123 long baseline = atomic_load(&live_allocations);
124 TEST_CHECK(baseline > 0);
125
126 mp_limb_t a[BIG_N], dst[2 * BIG_N];
127 mp_byte_t decimal[DECIMAL_CAP];
128 fill_big(a);
129 lmmp_mul_(dst, a, BIG_N, a, BIG_N);
130 big_to_decimal(decimal, a);
131
132 mp_bitcnt_t bits = 0;
134 mp_ptr fac = factorial_buffer(rn);
135 lmmp_factorial_(fac, bits, rn + 1, FACTORIAL_N);
136 free(fac);
137 TEST_CHECK(atomic_load(&live_allocations) > baseline);
138
140 TEST_CHECK(atomic_load(&live_allocations) == 0);
142 TEST_CHECK(atomic_load(&live_allocations) == baseline);
144 TEST_CHECK(atomic_load(&live_allocations) == 0);
145}
146
154
155#if defined(LMMP_WINDOWS)
156static unsigned __stdcall thread_worker(void *arg) {
157#else
158static void *thread_worker(void *arg) {
159#endif
160 thread_case *test = arg;
162
163 mp_byte_t decimal[DECIMAL_CAP];
164 bool match = big_to_decimal(decimal, test->input) == test->len &&
165 memcmp(decimal, test->decimal, test->len) == 0;
166 mp_bitcnt_t bits = 0;
168 mp_ptr fac = factorial_buffer(rn);
169 test->pass = match && rn == test->rn && bits == test->bits &&
170 lmmp_factorial_(fac, bits, rn + 1, FACTORIAL_N) > 0;
171 free(fac);
172
174 return 0;
175}
176
177TEST_CASE("global/thread", thread_isolation) {
178 enum { WORKERS = 4 };
179 mp_limb_t a[BIG_N];
180 mp_byte_t expected[DECIMAL_CAP], decimal[DECIMAL_CAP];
181 fill_big(a);
182 mp_size_t len = big_to_decimal(expected, a);
183 mp_bitcnt_t bits = 0;
185 thread_case cases[WORKERS];
186#if defined(LMMP_WINDOWS)
187 HANDLE workers[WORKERS];
188#else
189 pthread_t workers[WORKERS];
190#endif
191 size_t started = 0;
192 for (; started < WORKERS; ++started) {
193 cases[started] = (thread_case){a, expected, len, rn, bits, false};
194#if defined(LMMP_WINDOWS)
195 workers[started] = (HANDLE)_beginthreadex(NULL, 0, thread_worker, &cases[started], 0, NULL);
196 if (!workers[started])
197 break;
198#else
199 if (pthread_create(&workers[started], NULL, thread_worker, &cases[started]) != 0)
200 break;
201#endif
202 }
203 TEST_CHECK_EQ(started, WORKERS);
204
205 for (int repeat = 0; repeat < 8; ++repeat)
206 TEST_CHECK(big_to_decimal(decimal, a) == len && memcmp(decimal, expected, len) == 0);
207
208 for (size_t i = 0; i < started; ++i) {
209#if defined(LMMP_WINDOWS)
210 if (WaitForSingleObject(workers[i], INFINITE) != WAIT_OBJECT_0)
211 abort();
212 CloseHandle(workers[i]);
213#else
214 if (pthread_join(workers[i], NULL) != 0)
215 abort();
216#endif
217 TEST_CHECK_MSG(cases[i].pass, "worker thread result mismatch");
218 }
219 TEST_CHECK(big_to_decimal(decimal, a) == len && memcmp(decimal, expected, len) == 0);
220}
221
222void test_lifecycle(void) {
223 TEST_RUN("global/lifecycle", lifecycle_idempotent);
224 TEST_RUN("global/lifecycle", heap_balance_full_cycle);
225 TEST_RUN("global/thread", thread_isolation);
226}
void test_lifecycle(void)
Copyright (C) 2026 HJimmyK (Jericho Knox)
mp_limb_t * mp_ptr
定义 lmmp.h:96
uint8_t mp_byte_t
定义 lmmp.h:91
void lmmp_set_heap_allocator(const lmmp_heap_allocator_t *heap)
设置 LMMP 全局堆内存分配函数
size_t mp_bitcnt_t
定义 lmmp.h:98
uint64_t mp_size_t
定义 lmmp.h:93
const mp_limb_t * mp_srcptr
定义 lmmp.h:97
void lmmp_global_init(void)
全局初始化函数(线程局部的)
uint64_t mp_limb_t
定义 lmmp.h:92
void lmmp_global_deinit(void)
全局释放函数(线程局部的)
mp_size_t lmmp_to_str_(mp_byte_t *dst, mp_srcptr numa, mp_size_t na, int base)
limb数组转字节数组操作 [numa,na,B] to [dst,return value,base]
void lmmp_mul_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_srcptr numb, mp_size_t nb)
不等长乘法操作 [dst,na+nb] = [numa,na] * [numb,nb]
#define n
mp_size_t lmmp_factorial_size_(uint n, mp_bitcnt_t *bits)
计算 n!
mp_size_t lmmp_factorial_(mp_ptr dst, mp_bitcnt_t bits, mp_size_t rn, uint n)
计算 n!
#define TEST_CHECK(cond)
定义 test.h:37
#define TEST_CHECK_MSG(cond, msg)
定义 test.h:38
#define TEST_CASE(category, name)
定义 test.h:35
#define TEST_CHECK_EQ(a, b)
定义 test.h:39
#define TEST_RUN(category, name)
定义 test.h:36
static void * count_alloc(size_t n)
static mp_size_t big_to_decimal(mp_byte_t *out, mp_srcptr a)
const mp_byte_t * decimal
static void fill_big(mp_ptr a)
static mp_ptr factorial_buffer(mp_size_t n)
static void * count_realloc(void *p, size_t n)
static void count_free(void *p)
static unsigned __stdcall thread_worker(void *arg)
static atomic_long live_allocations
@ DECIMAL_CAP
@ FACTORIAL_N