Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

Commit 0d4d1d1

Browse files
committed
新增默认参数值和原始字符串字面量的测试示例,增强代码可读性和功能验证。
1 parent 9d6de7c commit 0d4d1d1

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using lib <io>;
2+
using ns std;
3+
4+
// 函数测试:默认参数值
5+
fn greet(name : string = "世界", greeting : string = "你好") : string {
6+
return `${greeting},${name}!`;
7+
};
8+
9+
fn buildConnection(host : string, port : int = 8080, timeout : int = 30) : string {
10+
return `连接到 ${host}:${port},超时: ${timeout}秒`;
11+
};
12+
13+
fn createPerson(name : string, age : int = 25, city : string = "北京") : string {
14+
return `姓名: ${name}, 年龄: ${age}, 城市: ${city}`;
15+
};
16+
17+
// 带表达式的默认值
18+
fn multiply(a : int, b : int = a * 2) : int {
19+
return a * b;
20+
};
21+
22+
fn formatDate(year : int, month : int = 1, day : int = 1) : string {
23+
// 月份和日期格式化(确保两位数)
24+
month_str : string = month < 10 ? "0" + month : "" + month;
25+
day_str : string = day < 10 ? "0" + day : "" + day;
26+
return `${year}-${month_str}-${day_str}`;
27+
};
28+
29+
// 原始字符串字面量测试
30+
fn testRawStringLiterals() : void {
31+
// 基本的原始字符串(不进行转义)
32+
path1 : string = "C:\\Users\\Documents\\file.txt"; // 常规字符串(需要转义)
33+
path2 : string = r"C:\Users\Documents\file.txt"; // 原始字符串(无需转义)
34+
35+
std::println("常规字符串 (已转义): " + path1);
36+
std::println("原始字符串 (未转义): " + path2);
37+
38+
// 特殊字符处理比较
39+
normal : string = "第一行\n第二行\t缩进";
40+
raw : string = r"第一行\n第二行\t缩进";
41+
42+
std::println("常规字符串 (特殊字符会被转义):");
43+
std::println(normal);
44+
std::println("原始字符串 (特殊字符保留字面值):");
45+
std::println(raw);
46+
47+
// 其他应用场景
48+
regex_pattern : string = r"\d+\.\d+"; // 正则表达式模式
49+
sql_query : string = r"SELECT * FROM users WHERE name LIKE '%user\_%'";
50+
51+
std::println("正则表达式模式: " + regex_pattern);
52+
std::println("SQL查询: " + sql_query);
53+
};
54+
55+
fn main() : int {
56+
std::println("=== 默认参数值测试 ===");
57+
58+
// 测试默认参数值
59+
std::println(greet()); // 使用两个默认参数
60+
std::println(greet("张三")); // 使用第二个默认参数
61+
std::println(greet("李四", "您好")); // 不使用默认参数
62+
63+
// 测试混合使用默认参数和必选参数
64+
std::println(buildConnection("localhost")); // 使用默认端口和超时
65+
std::println(buildConnection("example.com", 443)); // 使用默认超时
66+
std::println(buildConnection("server.net", 9000, 60)); // 不使用默认值
67+
68+
// 测试更复杂的默认值场景
69+
std::println(createPerson("王五")); // 使用默认年龄和城市
70+
std::println(createPerson("赵六", 30)); // 使用默认城市
71+
std::println(createPerson("钱七", 35, "上海")); // 不使用默认值
72+
73+
// 测试带表达式的默认值
74+
std::println("5 * (5*2) = " + multiply(5)); // 应该输出 5 * 10 = 50
75+
std::println("3 * 4 = " + multiply(3, 4)); // 应该输出 3 * 4 = 12
76+
77+
// 测试日期格式化
78+
std::println(formatDate(2025)); // 使用默认月份和日期
79+
std::println(formatDate(2025, 10)); // 使用默认日期
80+
std::println(formatDate(2025, 10, 15)); // 不使用默认值
81+
82+
std::println("\n=== 原始字符串字面量测试 ===");
83+
testRawStringLiterals();
84+
85+
return 0;
86+
};

0 commit comments

Comments
 (0)