Skip to content

Commit 6fb59b7

Browse files
authored
Merge pull request #71 from plctlab/testcases
增加测试用例框架及测试用例
2 parents f20dfc0 + 3affe0b commit 6fb59b7

Some content is hidden

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

54 files changed

+17571
-0
lines changed

.github/copilot-instructions.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Copilot Instructions for mlibc Project
2+
3+
## Project Overview / 项目概述
4+
5+
**English:**
6+
mlibc is a lightweight C library specifically designed for deeply embedded systems. The project focuses on implementing POSIX.1-2001 (PSE51) and related ANSI C APIs to provide essential system functionality for resource-constrained embedded environments. This library aims to offer a minimal yet complete implementation of standard C library functions while maintaining compatibility with various embedded architectures including RISC-V, ARM, and AArch64.
7+
8+
Key features:
9+
- PSE51 (POSIX.1-2001) compliance
10+
- ANSI C standard library implementation
11+
- Multi-architecture support (RISC-V32/64, ARM, AArch64)
12+
- Minimal memory footprint
13+
- Embedded-first design philosophy
14+
15+
**中文:**
16+
mlibc 是一个专门为深度嵌入式系统设计的轻量级 C 库。该项目专注于实现 POSIX.1-2001 (PSE51) 和相关的 ANSI C API,为资源受限的嵌入式环境提供基础系统功能。本库旨在提供标准 C 库函数的最小化但完整的实现,同时保持与多种嵌入式架构(包括 RISC-V、ARM 和 AArch64)的兼容性。
17+
18+
主要特性:
19+
- 符合 PSE51 (POSIX.1-2001) 标准
20+
- ANSI C 标准库实现
21+
- 多架构支持 (RISC-V32/64, ARM, AArch64)
22+
- 最小内存占用
23+
- 嵌入式优先的设计理念
24+
25+
## Code Review Instructions / 代码审查指南
26+
27+
### Review Language / 审查语言
28+
When performing code reviews, respond in Chinese and English.
29+
进行代码审查时,请使用中英文双语回复。
30+
31+
### Review Focus Areas / 审查重点
32+
33+
**English:**
34+
1. **Memory Safety**: Pay special attention to buffer overflows, memory leaks, and pointer safety
35+
2. **Embedded Constraints**: Consider memory usage, stack usage, and performance implications
36+
3. **Standard Compliance**: Ensure compliance with PSE51 and ANSI C standards
37+
4. **Architecture Portability**: Verify code works across supported architectures
38+
5. **Error Handling**: Check for proper error handling and edge cases
39+
6. **Documentation**: Ensure code is well-documented for embedded developers
40+
41+
**中文:**
42+
1. **内存安全**: 特别关注缓冲区溢出、内存泄漏和指针安全性
43+
2. **嵌入式约束**: 考虑内存使用、栈使用和性能影响
44+
3. **标准合规性**: 确保符合 PSE51 和 ANSI C 标准
45+
4. **架构可移植性**: 验证代码在支持的架构间正常工作
46+
5. **错误处理**: 检查适当的错误处理和边界情况
47+
6. **文档说明**: 确保代码有良好的文档供嵌入式开发者使用
48+
49+
### Review Format / 审查格式
50+
51+
Please structure your reviews as follows:
52+
请按以下格式组织您的审查:
53+
54+
```
55+
## Code Review / 代码审查
56+
57+
### Summary / 总结
58+
**English:** [Brief summary of changes and overall assessment]
59+
**中文:** [变更简要总结和整体评估]
60+
61+
### Issues Found / 发现的问题
62+
**English:**
63+
- [Issue 1 description]
64+
- [Issue 2 description]
65+
66+
**中文:**
67+
- [问题1描述]
68+
- [问题2描述]
69+
70+
### Recommendations / 建议
71+
**English:**
72+
- [Recommendation 1]
73+
- [Recommendation 2]
74+
75+
**中文:**
76+
- [建议1]
77+
- [建议2]
78+
79+
### Positive Aspects / 积极方面
80+
**English:**
81+
- [Good practice 1]
82+
- [Good practice 2]
83+
84+
**中文:**
85+
- [良好实践1]
86+
- [良好实践2]
87+
```
88+
89+
### Specific Considerations for mlibc / mlibc 特定考虑事项
90+
91+
**English:**
92+
- **PSE51 Compatibility**: Verify function signatures and behavior match PSE51 specifications
93+
- **Minimal Dependencies**: Avoid introducing unnecessary dependencies
94+
- **Cross-platform Build**: Consider SCons build system compatibility
95+
- **Test Coverage**: Ensure adequate test coverage in testcases/ directory
96+
- **Performance**: Optimize for embedded environments with limited resources
97+
98+
**中文:**
99+
- **PSE51 兼容性**: 验证函数签名和行为符合 PSE51 规范
100+
- **最小依赖**: 避免引入不必要的依赖
101+
- **跨平台构建**: 考虑 SCons 构建系统兼容性
102+
- **测试覆盖**: 确保 testcases/ 目录中有充分的测试覆盖
103+
- **性能优化**: 针对资源有限的嵌入式环境进行优化
104+
105+
### Common Patterns to Look For / 需要关注的常见模式
106+
107+
**English:**
108+
- Use of `__attribute__` for compiler-specific optimizations
109+
- Proper errno handling according to POSIX standards
110+
- Thread-safe implementations where required
111+
- Minimal stack usage in recursive functions
112+
- Proper header guard usage
113+
114+
**中文:**
115+
- 使用 `__attribute__` 进行编译器特定优化
116+
- 按照 POSIX 标准正确处理 errno
117+
- 在需要时实现线程安全
118+
- 递归函数中的最小栈使用
119+
- 正确使用头文件保护
120+
121+
### Quality Gates / 质量门槛
122+
123+
**English:**
124+
All code contributions should meet these criteria:
125+
- Compiles without warnings on all supported architectures
126+
- Passes existing test cases
127+
- Includes appropriate test coverage for new functionality
128+
- Maintains or improves performance benchmarks
129+
- Follows project coding style
130+
131+
**中文:**
132+
所有代码贡献都应满足以下标准:
133+
- 在所有支持的架构上无警告编译
134+
- 通过现有测试用例
135+
- 为新功能包含适当的测试覆盖
136+
- 维持或改善性能基准
137+
- 遵循项目编码风格

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Build files
22
build/
3+
build.log
4+
test_results
5+
36
toolchain/arm-linux-eabi_for_x86_64-pc-linux-gnu
47
toolchain/riscv64-unkwown-elf_for_x86_64-pc-linux-gnu
58
toolchain/binutils-*

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ endif
44

55
PROJECT_PATH := .
66

7+
# Set default toolchain if MLIBC_TOOLCHAIN is not set
8+
ifeq ($(MLIBC_TOOLCHAIN),)
9+
MLIBC_TOOLCHAIN := $(PROJECT_PATH)/toolchain/arm-linux-eabi_for_x86_64-pc-linux-gnu/bin/arm-linux-eabi-
10+
endif
11+
712
CC := $(MLIBC_TOOLCHAIN)gcc
813
AR := $(MLIBC_TOOLCHAIN)ar
914

testcases/Makefile

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
# Master Makefile for mlibc PSE51 Test Suite
2+
# Supports both PC and embedded test environments
3+
4+
# Test directories
5+
TEST_DIRS := assert ctype fcntl locale signal stat stdio stdlib string time unistd utsname
6+
7+
# Colors for output
8+
RED := \033[0;31m
9+
GREEN := \033[0;32m
10+
YELLOW := \033[1;33m
11+
BLUE := \033[0;34m
12+
NC := \033[0m
13+
14+
# Default target
15+
.PHONY: all
16+
all: help
17+
18+
# Help message
19+
.PHONY: help
20+
help:
21+
@echo "$(BLUE)=== mlibc PSE51 Test Suite ===$(NC)"
22+
@echo ""
23+
@echo "$(YELLOW)PC Environment Tests:$(NC)"
24+
@echo " make pc-build - Build all PC tests"
25+
@echo " make pc-test - Run all PC tests"
26+
@echo " make pc-clean - Clean PC test builds"
27+
@echo ""
28+
@echo "$(YELLOW)Embedded Environment Tests:$(NC)"
29+
@echo " make embedded-build - Build all embedded tests"
30+
@echo " make embedded-test - Run all embedded tests in QEMU"
31+
@echo " make embedded-clean - Clean embedded test builds"
32+
@echo ""
33+
@echo "$(YELLOW)Combined Commands:$(NC)"
34+
@echo " make build-all - Build both PC and embedded tests"
35+
@echo " make test-all - Run both PC and embedded tests"
36+
@echo " make clean-all - Clean all builds"
37+
@echo ""
38+
@echo "$(YELLOW)Quick Commands:$(NC)"
39+
@echo " make test - Run all PC tests (quick)"
40+
@echo " make embedded - Build and run embedded tests"
41+
@echo ""
42+
@echo "$(YELLOW)Individual Tests:$(NC)"
43+
@echo " make pc-test-<dir> - Run specific PC test (e.g., pc-test-stdio)"
44+
@echo " make embedded-test-<dir> - Run specific embedded test"
45+
46+
# PC Environment Targets
47+
.PHONY: pc-build
48+
pc-build:
49+
@echo "$(BLUE)Building all PC tests...$(NC)"
50+
@for dir in $(TEST_DIRS); do \
51+
if [ -f $$dir/Makefile ]; then \
52+
echo "$(YELLOW)Building $$dir...$(NC)"; \
53+
$(MAKE) -C $$dir pc --no-print-directory || exit 1; \
54+
fi; \
55+
done
56+
@echo "$(GREEN)All PC tests built successfully!$(NC)"
57+
58+
.PHONY: pc-test
59+
pc-test: pc-build
60+
@echo "$(BLUE)Running all PC tests...$(NC)"
61+
@total=0; passed=0; failed=0; \
62+
for dir in $(TEST_DIRS); do \
63+
if [ -f $$dir/Makefile ]; then \
64+
echo "$(YELLOW)\nTesting $$dir...$(NC)"; \
65+
if $(MAKE) -C $$dir test --no-print-directory; then \
66+
passed=$$((passed + 1)); \
67+
else \
68+
failed=$$((failed + 1)); \
69+
echo "$(RED)$$dir tests failed!$(NC)"; \
70+
fi; \
71+
total=$$((total + 1)); \
72+
fi; \
73+
done; \
74+
echo "\n$(BLUE)=== PC Test Summary ===$(NC)"; \
75+
echo "Total: $$total, Passed: $(GREEN)$$passed$(NC), Failed: $(RED)$$failed$(NC)"
76+
77+
.PHONY: pc-clean
78+
pc-clean:
79+
@echo "$(BLUE)Cleaning PC test builds...$(NC)"
80+
@for dir in $(TEST_DIRS); do \
81+
if [ -f $$dir/Makefile ]; then \
82+
$(MAKE) -C $$dir clean --no-print-directory; \
83+
fi; \
84+
done
85+
@echo "$(GREEN)PC tests cleaned!$(NC)"
86+
87+
# Embedded Environment Targets
88+
.PHONY: embedded-build
89+
embedded-build: check-mlibc
90+
@echo "$(BLUE)Building all embedded tests...$(NC)"
91+
@for dir in $(TEST_DIRS); do \
92+
if [ -f $$dir/Makefile.embedded ]; then \
93+
echo "$(YELLOW)Building embedded $$dir...$(NC)"; \
94+
$(MAKE) -C $$dir -f Makefile.embedded --no-print-directory || exit 1; \
95+
fi; \
96+
done
97+
@echo "$(GREEN)All embedded tests built successfully!$(NC)"
98+
99+
.PHONY: embedded-test
100+
embedded-test: embedded-build
101+
@echo "$(BLUE)Running embedded tests...$(NC)"
102+
@./test
103+
104+
.PHONY: embedded-clean
105+
embedded-clean:
106+
@echo "$(BLUE)Cleaning embedded test builds...$(NC)"
107+
@for dir in $(TEST_DIRS); do \
108+
if [ -f $$dir/Makefile.embedded ]; then \
109+
$(MAKE) -C $$dir -f Makefile.embedded clean --no-print-directory; \
110+
fi; \
111+
done
112+
@echo "$(GREEN)Embedded tests cleaned!$(NC)"
113+
114+
# Combined targets
115+
.PHONY: build-all
116+
build-all: pc-build embedded-build
117+
118+
.PHONY: test-all
119+
test-all: pc-test embedded-test
120+
121+
.PHONY: clean-all
122+
clean-all: pc-clean embedded-clean
123+
@rm -f embedded_test_results.log embedded_test_summary.txt
124+
125+
# Quick shortcuts
126+
.PHONY: test
127+
test: pc-test
128+
129+
.PHONY: embedded
130+
embedded: embedded-test
131+
132+
# Individual test targets
133+
define INDIVIDUAL_TEST_RULES
134+
.PHONY: pc-test-$(1)
135+
pc-test-$(1):
136+
@echo "$(BLUE)Testing $(1) (PC)...$(NC)"
137+
@if [ -f $(1)/Makefile ]; then \
138+
$(MAKE) -C $(1) test; \
139+
else \
140+
echo "$(RED)No PC tests for $(1)$(NC)"; \
141+
fi
142+
143+
.PHONY: embedded-test-$(1)
144+
embedded-test-$(1): check-mlibc
145+
@echo "$(BLUE)Testing $(1) (embedded)...$(NC)"
146+
@if [ -f $(1)/test ]; then \
147+
cd $(1) && ./test; \
148+
else \
149+
echo "$(RED)No embedded tests for $(1)$(NC)"; \
150+
fi
151+
endef
152+
153+
$(foreach dir,$(TEST_DIRS),$(eval $(call INDIVIDUAL_TEST_RULES,$(dir))))
154+
155+
# Check if mlibc is built
156+
.PHONY: check-mlibc
157+
check-mlibc:
158+
@ARCH=$${EMBEDDED_ARCH:-riscv32}; \
159+
if [ ! -f ../build/$$ARCH/libmlibc.a ]; then \
160+
if [ "$$ARCH" = "arm" ]; then \
161+
echo "$(YELLOW)Building mlibc for ARM...$(NC)"; \
162+
elif [ "$$ARCH" = "aarch64" ]; then \
163+
echo "$(YELLOW)Building mlibc for AArch64...$(NC)"; \
164+
elif [ "$$ARCH" = "riscv64" ]; then \
165+
echo "$(YELLOW)Building mlibc for RISC-V 64-bit...$(NC)"; \
166+
else \
167+
echo "$(YELLOW)Building mlibc for RISC-V 32-bit...$(NC)"; \
168+
fi; \
169+
$(MAKE) -C .. ARCH=$$ARCH || { \
170+
echo "$(RED)Failed to build mlibc!$(NC)"; \
171+
echo "Please ensure you have the $$ARCH toolchain installed."; \
172+
echo "Set MLIBC_TOOLCHAIN environment variable if needed."; \
173+
exit 1; \
174+
}; \
175+
fi
176+
177+
# Clean everything including logs
178+
.PHONY: distclean
179+
distclean: clean-all
180+
@rm -rf */build */test_results
181+
@rm -f */*.o */*.log
182+
@echo "$(GREEN)All test artifacts removed!$(NC)"
183+
184+
# Show test status
185+
.PHONY: status
186+
status:
187+
@echo "$(BLUE)=== Test Suite Status ===$(NC)"
188+
@echo "\n$(YELLOW)PC Tests:$(NC)"
189+
@for dir in $(TEST_DIRS); do \
190+
if [ -f $$dir/Makefile ]; then \
191+
if [ -d $$dir/test_results ]; then \
192+
echo " $$dir: $(GREEN)Built$(NC)"; \
193+
else \
194+
echo " $$dir: Not built"; \
195+
fi; \
196+
fi; \
197+
done
198+
@echo "\n$(YELLOW)Embedded Tests:$(NC)"
199+
@for dir in $(TEST_DIRS); do \
200+
if [ -f $$dir/Makefile.embedded ]; then \
201+
if [ -d $$dir/build ]; then \
202+
echo " $$dir: $(GREEN)Built$(NC)"; \
203+
else \
204+
echo " $$dir: Not built"; \
205+
fi; \
206+
fi; \
207+
done
208+
209+
# Install dependencies (informational)
210+
.PHONY: deps
211+
deps:
212+
@echo "$(BLUE)=== Dependencies ===$(NC)"
213+
@echo ""
214+
@echo "$(YELLOW)For PC tests:$(NC)"
215+
@echo " - GCC or compatible C compiler"
216+
@echo " - POSIX-compliant system"
217+
@echo ""
218+
@echo "$(YELLOW)For embedded tests:$(NC)"
219+
@echo " - RISC-V 32-bit toolchain (riscv32-unknown-elf-gcc)"
220+
@echo " - QEMU system emulator (qemu-system-riscv32)"
221+
@echo ""
222+
@echo "$(YELLOW)Ubuntu/Debian installation:$(NC)"
223+
@echo " sudo apt-get install gcc-riscv64-unknown-elf qemu-system-misc"
224+
@echo ""
225+
@echo "$(YELLOW)Environment variables:$(NC)"
226+
@echo " export MLIBC_TOOLCHAIN=riscv32-unknown-elf-"

0 commit comments

Comments
 (0)