Skip to content

Commit 14ea70e

Browse files
committed
feat: extract some typical test cases
1 parent c417f88 commit 14ea70e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+92
-8883
lines changed

.gitmodules

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
[submodule "package"]
22
path = package
3-
url = git@github.com:nature-lang/package.git
3+
url = https://github.com:nature-lang/package.git
44
[submodule "nls"]
55
path = nls
6-
url = git@github.com:nature-lang/nls.git
6+
url = https://github.com:nature-lang/nls.git
77
[submodule "tests/features/cases/20250405_02_compress"]
88
path = tests/features/cases/20250405_02_compress
9-
url = [email protected]:weiwenhao/compress.git
9+
url = https://github.com:weiwenhao/compress.git
10+
[submodule "tests/features/cases/20250324_00_llama"]
11+
path = tests/features/cases/20250324_00_llama
12+
url = https://github.com/weiwenhao/llama.n.git
13+
[submodule "tests/features/cases/20250404_00_tetris"]
14+
path = tests/features/cases/20250404_00_tetris
15+
url = https://github.com/weiwenhao/tetris.git
16+
[submodule "tests/features/cases/20250418_00_playground"]
17+
path = tests/features/cases/20250418_00_playground
18+
url = https://github.com/weiwenhao/playground.git
19+
[submodule "tests/features/cases/20250405_00_parker"]
20+
path = tests/features/cases/20250405_00_parker
21+
url = https://github.com/weiwenhao/parker.git

runtime/allocator.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ void *mheap_sys_alloc(mheap_t *mheap, uint64_t *size) {
566566
* @param span
567567
*/
568568
static void mheap_set_spans(mspan_t *span) {
569-
TDEBUGF("[mheap_set_spans] start, span=%p, base=%p, spc=%d, obj_size=%lu, pages_count=%lu", span,
569+
DEBUGF("[mheap_set_spans] start, span=%p, base=%p, spc=%d, obj_size=%lu, pages_count=%lu", span,
570570
(void *) span->base, span->spanclass,
571571
span->obj_size, span->pages_count);
572572

@@ -578,7 +578,7 @@ static void mheap_set_spans(mspan_t *span) {
578578
uint64_t page_index = (cursor_addr - arena->base) / ALLOC_PAGE_SIZE;
579579

580580
// 判断当前 page_index 是否已经被其他 span 占用,如果占用了
581-
TDEBUGF(
581+
DEBUGF(
582582
"[mheap_set_spans] arena_base=%p page_index=%lu will set span=%p, span_base=%p, cursor_addr=%p, page_count=%lu",
583583
(void *) arena->base,
584584
page_index, span, (void *) span->base, (void *) cursor_addr,
@@ -592,7 +592,7 @@ static void mheap_set_spans(mspan_t *span) {
592592
}
593593

594594
static void mheap_clear_spans(mspan_t *span) {
595-
TDEBUGF("[mheap_clear_spans] span=%p, base=%p, obj_size: %lu, pages_count: %lu", span, (void *) span->base,
595+
DEBUGF("[mheap_clear_spans] span=%p, base=%p, obj_size: %lu, pages_count: %lu", span, (void *) span->base,
596596
span->obj_size,
597597
span->pages_count);
598598

@@ -603,7 +603,7 @@ static void mheap_clear_spans(mspan_t *span) {
603603
uint64_t page_index = (cursor_addr - arena->base) / ALLOC_PAGE_SIZE;
604604

605605

606-
TDEBUGF("[mheap_clear_spans] arena_base: %p, page_index=%lu set span=%p, span_base=%p, pages_count=%ld",
606+
DEBUGF("[mheap_clear_spans] arena_base: %p, page_index=%lu set span=%p, span_base=%p, pages_count=%ld",
607607
(void *) arena->base, page_index, span,
608608
(void *) span->base, span->pages_count)
609609

@@ -842,7 +842,7 @@ static addr_t mcache_alloc(uint8_t spanclass, mspan_t **span) {
842842
bool used = bitmap_test(mspan->alloc_bits, i);
843843
if (used) {
844844
used_count += 1;
845-
// TDEBUGF("[runtime.mcache_alloc] obj_index=%d/%lu, used, continue", i, mspan->obj_count);
845+
// DEBUGF("[runtime.mcache_alloc] obj_index=%d/%lu, used, continue", i, mspan->obj_count);
846846
continue;
847847
}
848848

@@ -1040,7 +1040,7 @@ arena_hint_t *arena_hints_init() {
10401040
* @param span
10411041
*/
10421042
void mheap_free_span(mheap_t *mheap, mspan_t *span) {
1043-
TDEBUGF("[mheap_free_span] start, span->base=%p, pages_count=%lu, chunk_index=%lu", (void *) span->base,
1043+
DEBUGF("[mheap_free_span] start, span->base=%p, pages_count=%lu, chunk_index=%lu", (void *) span->base,
10441044
span->pages_count,
10451045
chunk_index(span->base));
10461046

@@ -1060,13 +1060,13 @@ void mheap_free_span(mheap_t *mheap, mspan_t *span) {
10601060
remove_total_bytes += span->pages_count * ALLOC_PAGE_SIZE;
10611061

10621062
// 将物理内存归还给操作系统
1063-
TDEBUGF("[mheap_free_span] remove_total_bytes=%lu MB, span.base=%p, span.pages_count=%ld, remove_size=%lu",
1063+
DEBUGF("[mheap_free_span] remove_total_bytes=%lu MB, span.base=%p, span.pages_count=%ld, remove_size=%lu",
10641064
remove_total_bytes / 1024 / 1024, (void *) span->base, span->pages_count,
10651065
span->pages_count * ALLOC_PAGE_SIZE);
10661066

10671067
sys_memory_unused((void *) span->base, span->pages_count * ALLOC_PAGE_SIZE);
10681068

1069-
TDEBUGF("[mheap_free_span] sys_memory_unused success");
1069+
DEBUGF("[mheap_free_span] sys_memory_unused success");
10701070
}
10711071

10721072
void memory_init() {
@@ -1199,7 +1199,7 @@ void *rti_gc_malloc(uint64_t size, rtype_t *rtype) {
11991199
mark_ptr_black(ptr);
12001200
}
12011201

1202-
TDEBUGF("[rti_gc_malloc] end p_index=%d, co=%p, result=%p, size=%d, hash=%d",
1202+
DEBUGF("[rti_gc_malloc] end p_index=%d, co=%p, result=%p, size=%d, hash=%d",
12031203
p->index, coroutine_get(), ptr, size, rtype ? rtype->hash : 0);
12041204

12051205
// jit span 不用清 0, 权限不足也无法进行清零
@@ -1327,7 +1327,7 @@ void *gc_malloc_size(uint64_t size) {
13271327

13281328

13291329
// uint64_t stage2 = uv_hrtime();
1330-
// TDEBUGF("[gc_malloc_size] malloc size is %lu, use time %lu ", size, stage2 - start);
1330+
// DEBUGF("[gc_malloc_size] malloc size is %lu, use time %lu ", size, stage2 - start);
13311331

13321332
void *result = rti_gc_malloc(size, NULL);
13331333
return result;

runtime/collector.c

+17-17
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,14 @@ static bool sweep_span(mcentral_t *central, mspan_t *span) {
183183
// 内存回收(未返回到堆)
184184
allocated_bytes -= span->obj_size;
185185

186-
TDEBUGF("[sweep_span] will sweep, span_base=%p obj_addr=%p", span->base, (void *) (span->base + i * span->obj_size));
186+
DEBUGF("[sweep_span] will sweep, span_base=%p obj_addr=%p", span->base, (void *) (span->base + i * span->obj_size));
187187

188188
// TODO 直接 set 0 让 gc 问题快速暴露出来, jit class 不允许设置内存,所以跳过
189189
if ((span->spanclass >> 1) != JIT_SIZECLASS) {
190190
memset((void *) (span->base + i * span->obj_size), 0, span->obj_size);
191191
}
192192
} else {
193-
TDEBUGF("[sweep_span] will sweep, span_base=%p, obj_addr=%p, not calc allocated_bytes, alloc_bit=%d, gcmark_bit=%d",
193+
DEBUGF("[sweep_span] will sweep, span_base=%p, obj_addr=%p, not calc allocated_bytes, alloc_bit=%d, gcmark_bit=%d",
194194
span->base,
195195
(void *) (span->base + i * span->obj_size), bitmap_test(span->alloc_bits, i),
196196
bitmap_test(span->gcmark_bits, i));
@@ -864,26 +864,26 @@ void runtime_gc() {
864864

865865
// - gc stage: GC_START
866866
gc_stage = GC_STAGE_START;
867-
TDEBUGF("[runtime_gc] start, allocated=%ldKB, gc stage: GC_START, pid %d", allocated_bytes / 1000, getpid());
867+
DEBUGF("[runtime_gc] start, allocated=%ldKB, gc stage: GC_START, pid %d", allocated_bytes / 1000, getpid());
868868

869869
memory->gc_count += 1;
870870

871871
// 等待所有的 processor 进入安全点
872872
processor_all_need_stop();
873873
if (!processor_all_wait_safe(GC_STW_WAIT_COUNT)) {
874-
TDEBUGF("[runtime_gc] wait processor safe timeout, will return")
874+
DEBUGF("[runtime_gc] wait processor safe timeout, will return")
875875
processor_all_start(); // 清空安全点
876876
// 重置 next gc bytes
877877
gc_stage = GC_STAGE_OFF;
878878
return;
879879
}
880880

881-
TDEBUGF("[runtime_gc] all processor safe");
881+
DEBUGF("[runtime_gc] all processor safe");
882882

883883
// 开启写屏障
884884
gc_barrier_start();
885885

886-
TDEBUGF("[runtime_gc] barrier start");
886+
DEBUGF("[runtime_gc] barrier start");
887887

888888
// 注入 GC 工作协程, gc_worklist 用来检测状态判断 gc work 是否全部完成
889889
// stw 开启后 work 才会工作
@@ -898,53 +898,53 @@ void runtime_gc() {
898898

899899
scan_pool();
900900

901-
TDEBUGF("[runtime_gc] gc work coroutine injected, will start the world");
901+
DEBUGF("[runtime_gc] gc work coroutine injected, will start the world");
902902
processor_all_start();
903903

904904
// - gc stage: GC_MARK
905905
gc_stage = GC_STAGE_MARK;
906-
TDEBUGF("[runtime_gc] gc stage: GC_MARK, the world start");
906+
DEBUGF("[runtime_gc] gc stage: GC_MARK, the world start");
907907

908908
// 等待所有的 processor 都 mark 完成
909909
wait_all_gc_work_finished();
910910

911911
// STW 之后再更改 GC 阶段
912-
TDEBUGF("[runtime_gc] wait all processor gc work completed, will stop the world and get solo stw locker");
912+
DEBUGF("[runtime_gc] wait all processor gc work completed, will stop the world and get solo stw locker");
913913
processor_all_need_stop();
914914
if (!processor_all_wait_safe(GC_STW_SWEEP_COUNT)) {
915-
TDEBUGF("[runtime_gc] wait processor safe sweep timeout, will return")
915+
DEBUGF("[runtime_gc] wait processor safe sweep timeout, will return")
916916
processor_all_start();
917917
gc_stage = GC_STAGE_OFF;
918918
return;
919919
}
920-
TDEBUGF("[runtime_gc] all processor safe");
920+
DEBUGF("[runtime_gc] all processor safe");
921921
// -------------- STW start ----------------------------
922922

923923
// - gc stage: GC_MARK_DONE
924924
gc_stage = GC_STAGE_MARK_DONE;
925-
TDEBUGF("[runtime_gc] gc stage: GC_MARK_DONE");
925+
DEBUGF("[runtime_gc] gc stage: GC_MARK_DONE");
926926

927927
// mark 完成期间还会存在新的 mutator barrier 产生的指针推送到 worklist 中
928928
// 所以必须等 STW 后进行最后的收尾
929929
gc_mark_done();
930930

931931
// - gc stage: GC_SWEEP
932932
gc_stage = GC_STAGE_SWEEP;
933-
TDEBUGF("[runtime_gc] gc stage: GC_SWEEP");
933+
DEBUGF("[runtime_gc] gc stage: GC_SWEEP");
934934

935935
// gc 清理 需要获取 memory_locker, 避免 wait_sysmon 中 processor_free 产生新的 uncache_span
936936
// 此时已经 stw 了,所以不需要使用 memory->locker
937937
flush_mcache();
938-
TDEBUGF("[runtime_gc] gc flush mcache completed");
938+
DEBUGF("[runtime_gc] gc flush mcache completed");
939939

940940
mcentral_sweep(memory->mheap);
941941

942942
flush_pool();
943943

944-
TDEBUGF("[runtime_gc] mcentral_sweep completed, unlock success");
944+
DEBUGF("[runtime_gc] mcentral_sweep completed, unlock success");
945945
// 更新 gcbits
946946
gcbits_arenas_epoch();
947-
TDEBUGF("[runtime_gc] gcbits_arenas_epoch completed, will stop gc barrier");
947+
DEBUGF("[runtime_gc] gcbits_arenas_epoch completed, will stop gc barrier");
948948

949949
gc_barrier_stop();
950950
processor_all_start();
@@ -953,7 +953,7 @@ void runtime_gc() {
953953
// 更新 next gc byts
954954
next_gc_bytes = _next_gc_bytes;
955955
gc_stage = GC_STAGE_OFF;
956-
TDEBUGF("[runtime_gc] gc stage: GC_OFF, gc_barrier_stop, current_allocated=%ldKB, cleanup=%ldKB",
956+
DEBUGF("[runtime_gc] gc stage: GC_OFF, gc_barrier_stop, current_allocated=%ldKB, cleanup=%ldKB",
957957
allocated_bytes / 1024,
958958
(before - allocated_bytes) / 1024);
959959
}

tests/CMakeLists.txt

+7-11
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,13 @@ include(CTest)
44
ENABLE_LANGUAGE(ASM)
55
FILE(GLOB_RECURSE RUNTIMES ../runtime/*c ../runtime/aco/acosw.S)
66

7-
if (CMAKE_SYSTEM_NAME STREQUAL "linux" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
8-
set(LIBUV_STATIC "${CMAKE_SOURCE_DIR}/lib/${CMAKE_SYSTEM_NAME}_${CMAKE_SYSTEM_PROCESSOR}/libuv_gcc.a")
9-
else ()
10-
set(LIBUV_STATIC "${CMAKE_SOURCE_DIR}/lib/${CMAKE_SYSTEM_NAME}_${CMAKE_SYSTEM_PROCESSOR}/libuv.a")
11-
endif ()
12-
message("LIBUV_STATIC: ${LIBUV_STATIC}")
13-
147
if (CMAKE_SYSTEM_NAME STREQUAL "darwin")
158
set(LIBC_STATIC "")
169
else ()
1710
set(LIBC_STATIC "${CMAKE_SOURCE_DIR}/lib/${CMAKE_SYSTEM_NAME}_${CMAKE_SYSTEM_PROCESSOR}/libc.a")
1811
endif ()
1912
message("LIBC_STATIC: ${LIBC_STATIC}")
2013

21-
add_executable(test_hash_table test_hash_table.c ${UTILS})
22-
target_link_libraries(test_hash_table ${LIBUV_STATIC})
23-
add_test(test_hash_table test_hash_table)
24-
2514
add_executable(test_arm64_encoding test_arm64_encoding.c ${UTILS} ${SRC})
2615
add_test(test_arm64_encoding test_arm64_encoding)
2716

@@ -74,6 +63,13 @@ endforeach()
7463
if (DEFINED ENV{RUNTIME_DEBUG_CASE})
7564
message("RUNTIME_DEBUG_CASE is defined: $ENV{RUNTIME_DEBUG_CASE}")
7665

66+
if (CMAKE_SYSTEM_NAME STREQUAL "linux" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
67+
set(LIBUV_STATIC "${CMAKE_SOURCE_DIR}/lib/${CMAKE_SYSTEM_NAME}_${CMAKE_SYSTEM_PROCESSOR}/libuv_gcc.a")
68+
else ()
69+
set(LIBUV_STATIC "${CMAKE_SOURCE_DIR}/lib/${CMAKE_SYSTEM_NAME}_${CMAKE_SYSTEM_PROCESSOR}/libuv.a")
70+
endif ()
71+
message("LIBUV_STATIC: ${LIBUV_STATIC}")
72+
7773
set(TEST_IDENT $ENV{RUNTIME_DEBUG_CASE})
7874
message("[test_runtime_debug] find RUNTIME_DEBUG_CASE: ${TEST_IDENT}")
7975

tests/features/20250418_00_playground.c

+13-11
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,19 @@ int main(void) {
104104
char curl_cmd[4096];
105105
char buf[2048] = {0};
106106

107-
snprintf(curl_cmd, sizeof(curl_cmd),
108-
"curl -s -X POST http://127.0.0.1:8888/playgrounds/run "
109-
"-H 'content-type: text/plain' --data-binary '%s'",
110-
code1);
111-
FILE *fp = popen(curl_cmd, "r");
112-
fread(buf, 1, sizeof(buf) - 1, fp);
113-
pclose(fp);
114-
if (!strstr(buf, "hello world\\nhello error\\n")) {
115-
kill(pid, SIGKILL);
116-
waitpid(pid, NULL, 0); // 等待子进程退出
117-
assertf(false, "buf: %s", buf);
107+
for (int i = 0; i < 20; ++i) {
108+
snprintf(curl_cmd, sizeof(curl_cmd),
109+
"curl -s -X POST http://127.0.0.1:8888/api/playgrounds/run"
110+
"-H 'content-type: text/plain' --data-binary '%s'",
111+
code1);
112+
FILE *fp = popen(curl_cmd, "r");
113+
fread(buf, 1, sizeof(buf) - 1, fp);
114+
pclose(fp);
115+
if (!strstr(buf, "hello world\\nhello error\\n")) {
116+
kill(pid, SIGKILL);
117+
waitpid(pid, NULL, 0); // 等待子进程退出
118+
assertf(false, "buf: %s", buf);
119+
}
118120
}
119121

120122
// kill pid

tests/features/cases/20250311_00_interface.testar

+23
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ update after: 3 4 50 50
235235
3 6
236236
update after: 5 1
237237

238+
239+
240+
238241
=== test_generics_impl
239242
--- main.n
240243
type measurable<T> = interface{
@@ -278,6 +281,9 @@ fn main():void! {
278281
--- output.txt
279282
nature-test/main.n:33:22: the fn 'perimeter' of type 'main.rectangle' mismatch interface 'main.measurable'
280283

284+
285+
286+
281287
=== test_typedef_arg_mismatch
282288
--- main.n
283289
type measurable<T> = interface{
@@ -321,6 +327,9 @@ fn main():void! {
321327
--- output.txt
322328
nature-test/main.n:35:24: typedef 'main.circle' args mismatch
323329

330+
331+
332+
324333
=== test_generics_interface
325334
--- main.n
326335
type measurable<T> = interface{
@@ -365,6 +374,9 @@ fn main():void! {
365374
12 14
366375
78 31
367376

377+
378+
379+
368380
=== test_interface_in_var
369381
--- main.n
370382
type measurable<T> = interface{
@@ -406,6 +418,7 @@ fn main():void! {
406418
78 31
407419

408420

421+
409422
=== test_struct_interface_default_value
410423
--- main.n
411424
type measurable<T> = interface{
@@ -449,6 +462,9 @@ fn main():void! {
449462
--- output.txt
450463
nature-test/main.n:35:18: struct filed 'm' must be assigned default value
451464

465+
466+
467+
452468
=== test_struct_interface
453469
--- main.n
454470
type measurable<T> = interface{
@@ -495,6 +511,9 @@ fn main():void! {
495511
--- output.txt
496512
12 14
497513

514+
515+
516+
498517
=== test_combination_impl_failed
499518
--- main.n
500519
type measurable<T> = interface{
@@ -522,6 +541,8 @@ fn main():void! {
522541
--- output.txt
523542
nature-test/main.n:14:2: type 'main.rectangle' not impl fn 'update' for interface 'main.updatable'
524543

544+
545+
525546
=== test_combination_impl_failed2
526547
--- main.n
527548
type measurable<T> = interface{
@@ -561,6 +582,8 @@ fn main():void! {
561582
--- output.txt
562583
nature-test/main.n:27:13: interface 'main.updatable' not declare 'area' fn
563584

585+
586+
564587
=== test_combination_impl
565588
--- main.n
566589
type measurable<T> = interface{
Submodule 20250324_00_llama added at 07e1246

tests/features/cases/20250324_00_llama/.gitignore

-2
This file was deleted.

tests/features/cases/20250324_00_llama/README.md

-1
This file was deleted.

0 commit comments

Comments
 (0)