LMMP
4.3.0
Lamina High-Precision Arithmetic Library
Toggle main menu visibility
载入中...
搜索中...
未找到
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
46
enum
{
BIG_N
= 64,
DECIMAL_CAP
= 20 *
BIG_N
+ 8,
FACTORIAL_N
= 65537 };
47
48
static
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
54
static
mp_size_t
big_to_decimal
(
mp_byte_t
*out,
mp_srcptr
a) {
55
return
lmmp_to_str_
(out, a,
BIG_N
, 10);
56
}
57
58
static
mp_ptr
factorial_buffer
(
mp_size_t
n
) {
59
mp_ptr
buffer = calloc(
n
+ 1,
sizeof
(*buffer));
60
if
(!buffer)
61
abort();
62
return
buffer;
63
}
64
65
TEST_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
70
lmmp_global_init
();
71
lmmp_global_init
();
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;
78
mp_size_t
rn =
lmmp_factorial_size_
(
FACTORIAL_N
, &bits);
79
mp_ptr
fac =
factorial_buffer
(rn);
80
TEST_CHECK
(
lmmp_factorial_
(fac, bits, rn + 1,
FACTORIAL_N
) > 0);
81
82
lmmp_global_deinit
();
83
lmmp_global_deinit
();
84
lmmp_global_init
();
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;
91
mp_size_t
rn2 =
lmmp_factorial_size_
(
FACTORIAL_N
, &bits2);
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
100
static
atomic_long
live_allocations
;
101
102
static
void
*
count_alloc
(
size_t
n
) {
103
void
*p = malloc(
n
);
104
if
(p)
105
++
live_allocations
;
106
return
p;
107
}
108
109
static
void
count_free
(
void
*p) {
110
if
(p)
111
--
live_allocations
;
112
free(p);
113
}
114
115
static
void
*
count_realloc
(
void
*p,
size_t
n
) {
116
return
p ? realloc(p,
n
) :
count_alloc
(
n
);
117
}
118
119
TEST_CASE
(
"global/lifecycle"
, heap_balance_full_cycle) {
120
const
lmmp_heap_allocator_t
counting = {
count_alloc
,
count_free
,
count_realloc
};
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;
133
mp_size_t
rn =
lmmp_factorial_size_
(
FACTORIAL_N
, &bits);
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
139
lmmp_global_deinit
();
140
TEST_CHECK
(atomic_load(&
live_allocations
) == 0);
141
lmmp_global_init
();
142
TEST_CHECK
(atomic_load(&
live_allocations
) == baseline);
143
lmmp_set_heap_allocator
(&libc);
144
TEST_CHECK
(atomic_load(&
live_allocations
) == 0);
145
}
146
147
typedef
struct
{
148
mp_srcptr
input
;
149
const
mp_byte_t
*
decimal
;
150
mp_size_t
len
,
rn
;
151
mp_bitcnt_t
bits
;
152
bool
pass
;
153
}
thread_case
;
154
155
#if defined(LMMP_WINDOWS)
156
static
unsigned
__stdcall
thread_worker
(
void
*arg) {
157
#else
158
static
void
*
thread_worker
(
void
*arg) {
159
#endif
160
thread_case
*test = arg;
161
lmmp_global_init
();
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;
167
mp_size_t
rn =
lmmp_factorial_size_
(
FACTORIAL_N
, &bits);
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
173
lmmp_global_deinit
();
174
return
0;
175
}
176
177
TEST_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;
184
mp_size_t
rn =
lmmp_factorial_size_
(
FACTORIAL_N
, &bits);
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
222
void
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
}
cases.h
test_lifecycle
void test_lifecycle(void)
Copyright (C) 2026 HJimmyK (Jericho Knox)
定义
test_lifecycle.c:222
lmmp.h
mp_ptr
mp_limb_t * mp_ptr
定义
lmmp.h:96
mp_byte_t
uint8_t mp_byte_t
定义
lmmp.h:91
lmmp_set_heap_allocator
void lmmp_set_heap_allocator(const lmmp_heap_allocator_t *heap)
设置 LMMP 全局堆内存分配函数
定义
memory.c:100
mp_bitcnt_t
size_t mp_bitcnt_t
定义
lmmp.h:98
mp_size_t
uint64_t mp_size_t
定义
lmmp.h:93
mp_srcptr
const mp_limb_t * mp_srcptr
定义
lmmp.h:97
lmmp_global_init
void lmmp_global_init(void)
全局初始化函数(线程局部的)
定义
global.c:58
mp_limb_t
uint64_t mp_limb_t
定义
lmmp.h:92
lmmp_global_deinit
void lmmp_global_deinit(void)
全局释放函数(线程局部的)
定义
global.c:65
lmmp_heap_allocator_t
定义
lmmp.h:239
lmmpn.h
lmmp_to_str_
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]
定义
to_str.c:169
lmmp_mul_
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]
n
#define n
numth.h
lmmp_factorial_size_
mp_size_t lmmp_factorial_size_(uint n, mp_bitcnt_t *bits)
计算 n!
lmmp_factorial_
mp_size_t lmmp_factorial_(mp_ptr dst, mp_bitcnt_t bits, mp_size_t rn, uint n)
计算 n!
TEST_CHECK
#define TEST_CHECK(cond)
定义
test.h:37
TEST_CHECK_MSG
#define TEST_CHECK_MSG(cond, msg)
定义
test.h:38
TEST_CASE
#define TEST_CASE(category, name)
定义
test.h:35
TEST_CHECK_EQ
#define TEST_CHECK_EQ(a, b)
定义
test.h:39
TEST_RUN
#define TEST_RUN(category, name)
定义
test.h:36
count_alloc
static void * count_alloc(size_t n)
定义
test_lifecycle.c:102
thread_case::rn
mp_size_t rn
定义
test_lifecycle.c:150
big_to_decimal
static mp_size_t big_to_decimal(mp_byte_t *out, mp_srcptr a)
定义
test_lifecycle.c:54
thread_case::decimal
const mp_byte_t * decimal
定义
test_lifecycle.c:149
fill_big
static void fill_big(mp_ptr a)
定义
test_lifecycle.c:48
factorial_buffer
static mp_ptr factorial_buffer(mp_size_t n)
定义
test_lifecycle.c:58
count_realloc
static void * count_realloc(void *p, size_t n)
定义
test_lifecycle.c:115
thread_case::len
mp_size_t len
定义
test_lifecycle.c:150
count_free
static void count_free(void *p)
定义
test_lifecycle.c:109
thread_worker
static unsigned __stdcall thread_worker(void *arg)
定义
test_lifecycle.c:156
thread_case::input
mp_srcptr input
定义
test_lifecycle.c:148
thread_case::bits
mp_bitcnt_t bits
定义
test_lifecycle.c:151
thread_case::pass
bool pass
定义
test_lifecycle.c:152
live_allocations
static atomic_long live_allocations
定义
test_lifecycle.c:100
DECIMAL_CAP
@ DECIMAL_CAP
定义
test_lifecycle.c:46
FACTORIAL_N
@ FACTORIAL_N
定义
test_lifecycle.c:46
BIG_N
@ BIG_N
定义
test_lifecycle.c:46
thread_case
定义
test_lifecycle.c:147
src
lmmp
lmmp
test
test_lifecycle.c
生成于
, 为 LMMP使用
1.18.0