chsrc/test/xy.c
2025-08-26 17:50:16 +08:00

177 lines
6.3 KiB
C

/** ------------------------------------------------------------
* SPDX-License-Identifier: MIT
* -------------------------------------------------------------
* Test File : xy.c
* Test Authors : 曾奥然 <ccmywish@qq.com>
* Contributors : Mikachu2333 <mikachu.23333@zohomail.com>
* Created On : <2023-08-30>
* Last Modified : <2025-08-26>
*
* Test xy.h
* ------------------------------------------------------------*/
#include "xy.h"
void
callback_print_str_for_seq (void *str, void *DUMMY)
{
print ((char *) str);
}
void
callback_print_str_for_map (const char *key, void *value, void *DUMMY)
{
println (xy_strcat (4, " ", key, " => ", (char *) value));
}
int
main (int argc, char const *argv[])
{
xy_init ();
println (xy_os_depend_str ("Hello, Windows!", "Hello, Unix!"));
println (3);
double dbl = 3.1415;
println (dbl);
say (xy_2strcat ("Xi", "'an"));
say (xy_strcat (2, "Xi", "'an"));
say (xy_strcat (3, "屈身守分,", "以待天时,", "不可与命争也"));
say (xy_strcat (4, "水落鱼梁浅,", "天寒梦泽深。", "羊公碑字在,", "读罢泪沾襟。"));
say (xy_strcat (6, "楚山横地出,", "汉水接天回。", "冠盖非新里,", "章华即旧台。", "习池风景异,", "归路满尘埃。"));
print (xy_str_to_bold ("粗体"));
print (xy_str_to_faint ("浅体"));
print (xy_str_to_italic ("斜体"));
print (xy_str_to_underline ("下划线"));
print (xy_str_to_blink ("闪烁"));
print (xy_str_to_cross ("删除线"));
print (xy_str_to_red ("红色"));
print (xy_str_to_green ("绿色"));
print (xy_str_to_yellow ("黄色"));
print (xy_str_to_blue ("蓝色"));
print (xy_str_to_magenta ("紫色"));
print (xy_str_to_purple ("紫色"));
print (xy_str_to_cyan ("青色"));
br();
xy_log ("普通", "输出普通内容");
xy_succ ("成功", "输出成功内容");
xy_info ("信息", "输出信息内容");
xy_warn ("警告", "输出警告内容");
xy_error ("错误", "输出错误内容");
xy_log_brkt ("xy.h", "普通", "咸阳油泼面筋道十足辣子香");
xy_succ_brkt ("xy.h", "成功", "西安花干鸡蛋肉夹馍已出炉");
xy_info_brkt ("xy.h", "信息", "襄阳牛肉面搭配黄酒更美味");
xy_warn_brkt ("xy.h", "警告", "兰州牛肉面,而非兰州拉面");
xy_error_brkt ("xy.h", "错误", "西安肉丸胡辣汤里没有肉丸");
assert (xy_streql ("abc", "abc"));
assert (xy_streql_ic ("abc", "abc"));
assert (false == xy_streql ("abc", "abC"));
assert (true == xy_streql_ic ("abc", "abC"));
assert (false == xy_str_end_with ("abcdef", "abcdefg"));
assert (xy_str_end_with ("abcdef", "def"));
assert (xy_str_end_with ("abcdef", "bcdef"));
assert (xy_str_end_with ("abcdef", "abcdef"));
assert (xy_str_end_with ("abcdef", ""));
assert (false == xy_str_start_with ("abcdef", "abcdefg"));
assert (xy_str_start_with ("abcdef", "abc"));
assert (xy_str_start_with ("abcdef", "abcde"));
assert (xy_str_start_with ("abcdef", "abcdef"));
assert (xy_str_start_with ("abcdef", ""));
assert_str ("abcdefg", xy_str_delete_suffix ("abcdefg", "cdef"));
assert_str ("abcdefg", xy_str_delete_suffix ("abcdefg", "cdefgh"));
assert_str ("abcdefg", xy_str_delete_suffix ("abcdefg", ""));
assert_str ("abcd", xy_str_delete_suffix ("abcdefg", "efg"));
assert_str ("abcdefg", xy_str_delete_prefix ("abcdefg", "cdef"));
assert_str ("abcdefg", xy_str_delete_prefix ("abcdefg", "0abcde"));
assert_str ("abcdefg", xy_str_delete_prefix ("abcdefg", ""));
assert_str ("defg", xy_str_delete_prefix ("abcdefg", "abc"));
assert_str ("defdef", xy_str_gsub ("abcdefabcdef", "abc", "")); // 删除
assert_str ("6def6def", xy_str_gsub ("abcdefabcdef", "abc", "6")); // 缩小
assert_str ("XIANGdefXIANGdef",
xy_str_gsub ("abcdefabcdef", "abc", "XIANG")); // 扩张
assert_str ("DEFdefDEFdef",
xy_str_gsub ("abcdefabcdef", "abc", "DEF")); // 等量
assert (xy_file_exist ("./doc/image/chsrc.png"));
assert (xy_dir_exist ("~"));
if (xy_on_windows)
{
say (xy_normalize_path ("~"));
say (xy_normalize_path ("~/"));
// xy_parent_dir() 得到的结果一定是不包含尾斜杠的目录名
assert_str (xy_parent_dir (xy_normalize_path ("~")), "C:\\Users");
assert_str (xy_parent_dir (xy_normalize_path ("~/")), "C:\\Users");
assert (xy_dir_exist ("C:\\Users"));
say (xy_win_powershell_profile);
say (xy_win_powershellv5_profile);
assert (xy_file_exist (xy_win_powershell_profile));
assert (true == xy_file_exist (xy_win_powershellv5_profile));
assert_str (xy_normalize_path ("C:\\a bc\\def\\"), "C:\\a bc\\def\\");
assert_str (xy_normalize_path ("a/b c/d"), "a\\b c\\d");
assert_str (xy_normalize_path ("a/b c/d/"), "a\\b c\\d\\");
assert_str (xy_parent_dir ("a/b c/d"), "a\\b c");
assert_str (xy_parent_dir ("a/b c\\d/"), "a\\b c");
}
else
{
/**
* debuild 过程会创建虚拟的 HOME 环境,导致检查 .bashrc 的测试会失败,所以我们先检查一下 .profile
* 如果没有,则大概率也没有 .bashrc
*/
if (xy_file_exist ("~/.profile"))
{
assert (xy_file_exist (xy_bashrc));
}
assert (xy_dir_exist ("/etc"));
}
println (xy_normalize_path (" \n ~/haha/test/123 \n\r "));
assert_str (xy_normalize_path ("~/haha/test"), xy_parent_dir (" ~/haha/test/123"));
XySeq_t *seq = xy_seq_new ();
xy_seq_push (seq, "Hello");
xy_seq_push (seq, "World");
assert_str ("Hello", xy_seq_at (seq, 1));
assert_str ("World", xy_seq_at (seq, 2));
xy_seq_each (seq, callback_print_str_for_seq, NULL); br();
xy_seq_pop (seq);
assert (1 == xy_seq_len (seq));
XyMap_t *map = xy_map_new ();
xy_map_set (map, "Hello", "World");
xy_map_set (map, "你好", "世界");
assert_str ("World", xy_map_get (map, "Hello"));
assert_str ("世界", xy_map_get (map, "你好"));
xy_map_set (map, "Hello", "chsrc");
assert_str ("chsrc", xy_map_get (map, "Hello"));
assert (2 == xy_map_len (map));
echo ("{");
xy_map_each (map, callback_print_str_for_map, NULL);
echo ("}");
xy_succ ("测试完成", "xy.h 测试全部通过");
// xy_unimplemented();
// xy_unsupported();
// xy_unreached();
return 0;
}