| 1 |
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
| 2 |
/*
|
| 3 |
* test_lml
|
| 4 |
* - tester for LML render
|
| 5 |
*
|
| 6 |
* Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com>
|
| 7 |
*/
|
| 8 |
|
| 9 |
#ifdef HAVE_CONFIG_H
|
| 10 |
#include "config.h"
|
| 11 |
#endif
|
| 12 |
|
| 13 |
#include "lml.h"
|
| 14 |
#include "log.h"
|
| 15 |
#include "trie_dict.h"
|
| 16 |
#include <errno.h>
|
| 17 |
#include <locale.h>
|
| 18 |
#include <stdio.h>
|
| 19 |
#include <string.h>
|
| 20 |
#include <time.h>
|
| 21 |
#include <unistd.h>
|
| 22 |
|
| 23 |
enum _test_lml_constant_t
|
| 24 |
{
|
| 25 |
STR_OUT_BUF_SIZE = 4096,
|
| 26 |
};
|
| 27 |
|
| 28 |
static const char TRIE_DICT_SHM_FILE[] = "~trie_dict_shm.dat";
|
| 29 |
|
| 30 |
const char *str_in[] = {
|
| 31 |
"[left]ABCD[right]EFG",
|
| 32 |
"A[u]B[italic]CD[/i]E[/u]F[b]G[/bold]",
|
| 33 |
"A[url BC DE]测试a网址[/url]FG",
|
| 34 |
"AB[email CDE]F[/eMAil]G01[emaiL]23456[/email]789",
|
| 35 |
"A[user DE]BC[/User]FG",
|
| 36 |
"[article A B CD]EF[ /article]G[article 789]123[/article]456",
|
| 37 |
"A[ image BCD]EFG",
|
| 38 |
"AB[ Flash CDE ]FG",
|
| 39 |
"AB[bwf]CDEFG",
|
| 40 |
"[lef]A[rightBCD[right]EF[left[left[]G[left",
|
| 41 |
"A[ color BCD]EF[/color]G[color black]0[/color][color magenta]1[color cyan]23[/color]4[color red]5[/color]6[color yellOw]7[/color]8[color green]9[color blue]0[/color]",
|
| 42 |
"A[quote]B[quote]C[quote]D[quote]E[/quote]F[/quote]G[/quote]0[/quote]1[/quote]2[quote]3[/quote]4[/quote]56789",
|
| 43 |
": ABCDE[quote]FG\r\nab[/quote]cd[quote]ef[quote]g\r\n: : 012[/quote]345[/quote]6789\nABC[quote]DEFG",
|
| 44 |
"\033[1;35;42mABC\033[0mDE\033[334mF\033[33mG\033[12345\033[m",
|
| 45 |
"123456",
|
| 46 |
"[color red]Red[/color][plain][color blue]Blue[/color][plain]",
|
| 47 |
"[color yellow]Yellow[/color][nolml][left][color blue]Blue[/color][right][lml][color red]Red[/color]",
|
| 48 |
"[abc][left ][ right ][ colory ][left \nABCD[left]EFG[right ",
|
| 49 |
"ABCD]EFG",
|
| 50 |
": : A123456789B123456789C123456789D123456789E123456789F123456789G123456789H123456789I123456789J123456789",
|
| 51 |
"\033[0m\033[I \033[1;32m;, ;,\033[m",
|
| 52 |
"\n01234567890123456789012345678901234567890123456789012345678901234567890123456789\n2\n01234567890123456789012345678901234567890123456789012345678901234567890123456789\n4\n5\n",
|
| 53 |
"A[012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789]B",
|
| 54 |
"[nolml]发信人:feilan.bbs@bbs.sjtu.edu.cn (蓝,本是路人),信区:cn.bbs.sci.medicine\n"
|
| 55 |
"标 题:Re: 阑尾有益寿延年功能 尾骨是有用的“小尾巴”\n"
|
| 56 |
"发信站:饮水思源\n"
|
| 57 |
"转信站:LeafOK!news.ccie.net.cn!SJTU\n"
|
| 58 |
"\n"
|
| 59 |
"昏\n"
|
| 60 |
"当然是阑尾\n"
|
| 61 |
"【 在 lang (浪子~继续减肥&戒酒) 的大作中提到: 】\n"
|
| 62 |
": 阑尾?\n"
|
| 63 |
": 扁桃体?\n"
|
| 64 |
": 还是尾椎?\n"
|
| 65 |
": 【 在 feilan (蓝,本是路人) 的大作中提到: 】\n"
|
| 66 |
": : -________________-!!!\n"
|
| 67 |
": : 完了\n"
|
| 68 |
": : 我已经割掉了\n"
|
| 69 |
": : 555555555555\n"
|
| 70 |
": : ",
|
| 71 |
"[image http://us.ent4.yimg.com/movies.yahoo.com/images/hv/photo/movie_pix/images/hv/photo/movie_pix/]\n",
|
| 72 |
"[tag-name-buffer-overflow abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]\n",
|
| 73 |
};
|
| 74 |
|
| 75 |
const int str_cnt = sizeof(str_in) / sizeof(const char *);
|
| 76 |
|
| 77 |
int main(int argc, char *argv[])
|
| 78 |
{
|
| 79 |
clock_t clock_begin;
|
| 80 |
clock_t clock_end;
|
| 81 |
double prog_time_spent;
|
| 82 |
double lml_time_spent;
|
| 83 |
|
| 84 |
char str_out_buf[STR_OUT_BUF_SIZE];
|
| 85 |
FILE *fp;
|
| 86 |
int i;
|
| 87 |
int j;
|
| 88 |
|
| 89 |
// Apply the specified locale
|
| 90 |
if (setlocale(LC_ALL, "en_US.UTF-8") == NULL)
|
| 91 |
{
|
| 92 |
fprintf(stderr, "setlocale(LC_ALL, en_US.UTF-8) error\n");
|
| 93 |
return -1;
|
| 94 |
}
|
| 95 |
|
| 96 |
if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
|
| 97 |
{
|
| 98 |
printf("Open log error\n");
|
| 99 |
return -1;
|
| 100 |
}
|
| 101 |
|
| 102 |
log_common_redir(STDOUT_FILENO);
|
| 103 |
log_error_redir(STDERR_FILENO);
|
| 104 |
|
| 105 |
if ((fp = fopen(TRIE_DICT_SHM_FILE, "w")) == NULL)
|
| 106 |
{
|
| 107 |
log_error("fopen(%s) error", TRIE_DICT_SHM_FILE);
|
| 108 |
return -1;
|
| 109 |
}
|
| 110 |
fclose(fp);
|
| 111 |
|
| 112 |
if (trie_dict_init(TRIE_DICT_SHM_FILE, TRIE_NODE_PER_POOL) < 0)
|
| 113 |
{
|
| 114 |
printf("trie_dict_init failed\n");
|
| 115 |
return -1;
|
| 116 |
}
|
| 117 |
|
| 118 |
clock_begin = clock();
|
| 119 |
|
| 120 |
if (lml_init() < 0)
|
| 121 |
{
|
| 122 |
printf("lml_init() error\n");
|
| 123 |
log_end();
|
| 124 |
return -1;
|
| 125 |
}
|
| 126 |
|
| 127 |
printf("Test #1: quote_mode = 0\n");
|
| 128 |
for (i = 0; i < str_cnt; i++)
|
| 129 |
{
|
| 130 |
j = lml_render(str_in[i], str_out_buf, sizeof(str_out_buf), 80, 0);
|
| 131 |
|
| 132 |
printf("Input(len=%ld): %s\nOutput(len=%d): %s\n",
|
| 133 |
strlen(str_in[i]), str_in[i], j, str_out_buf);
|
| 134 |
if (j != strlen(str_out_buf))
|
| 135 |
{
|
| 136 |
printf("Output len(%ld) != ret(%d)\n", strlen(str_out_buf), j);
|
| 137 |
return -1;
|
| 138 |
}
|
| 139 |
}
|
| 140 |
printf("Test #1: Done\n\n");
|
| 141 |
|
| 142 |
printf("Test #2: quote_mode = 1\n");
|
| 143 |
for (i = 0; i < str_cnt; i++)
|
| 144 |
{
|
| 145 |
j = lml_render(str_in[i], str_out_buf, sizeof(str_out_buf), 80, 1);
|
| 146 |
|
| 147 |
printf("Input(len=%ld): %s\nOutput(len=%d): %s\n",
|
| 148 |
strlen(str_in[i]), str_in[i], j, str_out_buf);
|
| 149 |
if (j != strlen(str_out_buf))
|
| 150 |
{
|
| 151 |
printf("Output len(%ld) != ret(%d)\n", strlen(str_out_buf), j);
|
| 152 |
return -1;
|
| 153 |
}
|
| 154 |
}
|
| 155 |
printf("Test #2: Done\n\n");
|
| 156 |
|
| 157 |
clock_end = clock();
|
| 158 |
prog_time_spent = (double)(clock_end - clock_begin) / (CLOCKS_PER_SEC / 1000);
|
| 159 |
lml_time_spent = (double)lml_total_exec_duration / (CLOCKS_PER_SEC / 1000);
|
| 160 |
|
| 161 |
printf("\npage_exec_duration=%.2f, lml_exec_duration=%.2f\n", prog_time_spent, lml_time_spent);
|
| 162 |
|
| 163 |
lml_cleanup();
|
| 164 |
trie_dict_cleanup();
|
| 165 |
|
| 166 |
if (unlink(TRIE_DICT_SHM_FILE) < 0)
|
| 167 |
{
|
| 168 |
log_error("unlink(%s) error", TRIE_DICT_SHM_FILE);
|
| 169 |
return -1;
|
| 170 |
}
|
| 171 |
|
| 172 |
log_end();
|
| 173 |
|
| 174 |
return 0;
|
| 175 |
}
|