Skip to content

Commit 9f8c5e0

Browse files
DwarfMasonLinuxJedi
authored andcommitted
Add break statement in mysqltest
1 parent f5aae71 commit 9f8c5e0

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

client/mysqltest.cc

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ enum enum_commands {
354354
Q_INC, Q_DEC,
355355
Q_SOURCE, Q_DISCONNECT,
356356
Q_LET, Q_ECHO,
357-
Q_WHILE, Q_END_BLOCK,
357+
Q_WHILE, Q_END_BLOCK, Q_BREAK,
358358
Q_SYSTEM, Q_RESULT,
359359
Q_REQUIRE, Q_SAVE_MASTER_POS,
360360
Q_SYNC_WITH_MASTER,
@@ -424,6 +424,7 @@ const char *command_names[]=
424424
"echo",
425425
"while",
426426
"end",
427+
"break",
427428
"system",
428429
"result",
429430
"require",
@@ -6306,6 +6307,33 @@ enum block_op find_operand(const char *start)
63066307
return ILLEG_OP;
63076308
}
63086309

6310+
/*
6311+
do_break
6312+
6313+
DESCRIPTION
6314+
Instruction to stop execution of the current loop
6315+
*/
6316+
void do_break(struct st_command* command)
6317+
{
6318+
int depth= 0;
6319+
cur_block->ok= false;
6320+
6321+
/* Disable every outer block until while found or block stack ends */
6322+
while (cur_block->cmd != cmd_while && cur_block > block_stack)
6323+
{
6324+
cur_block--;
6325+
cur_block->ok= false;
6326+
depth++;
6327+
}
6328+
6329+
/* Check if the top block is not 'while' */
6330+
if (cur_block->cmd != cmd_while)
6331+
{
6332+
die("Stray break was found");
6333+
}
6334+
/* Set current block back */
6335+
cur_block+= depth;
6336+
}
63096337

63106338
/*
63116339
Process start of a "if" or "while" statement
@@ -10059,6 +10087,7 @@ int main(int argc, char **argv)
1005910087
case Q_INC: do_modify_var(command, DO_INC); break;
1006010088
case Q_DEC: do_modify_var(command, DO_DEC); break;
1006110089
case Q_ECHO: do_echo(command); command_executed++; break;
10090+
case Q_BREAK: do_break(command); break;
1006210091
case Q_SYSTEM: do_system(command); break;
1006310092
case Q_REMOVE_FILE: do_remove_file(command); break;
1006410093
case Q_REMOVE_FILES_WILDCARD: do_remove_files_wildcard(command); break;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
3
2+
2
3+
OK
4+
OK
5+
OK
6+
1
7+
cnt=3
8+
cnt=2
9+
mysqltest: At line 1: Stray break was found

mysql-test/main/mysqltest-break.test

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#
2+
# MDEV-12130 improve mysqltest language
3+
#
4+
# test "break" statement
5+
#
6+
7+
# Break in a single loop
8+
9+
let $cnt= 4;
10+
while($cnt > 1)
11+
{
12+
dec $cnt;
13+
break;
14+
--echo $cnt
15+
--echo Break did not stop a single loop
16+
}
17+
18+
# Break stops inner loop
19+
20+
let $outer= 4;
21+
while($outer > 1)
22+
{
23+
let $inner= 4;
24+
while($inner > 1)
25+
{
26+
if($outer == 2)
27+
{
28+
--echo OK
29+
}
30+
if($inner == 2)
31+
{
32+
break;
33+
}
34+
dec $inner;
35+
}
36+
dec $outer;
37+
--echo $outer
38+
}
39+
40+
# Break stops outer loop
41+
let $inner= 4;
42+
let $outer= 4;
43+
while($outer > 1)
44+
{
45+
break;
46+
while($inner > 1)
47+
{
48+
dec $inner;
49+
--echo Outer loop`s break did not stop inner loop
50+
}
51+
dec $outer;
52+
--echo $outer
53+
}
54+
55+
# Break stops loop in if
56+
let $cnt= 4;
57+
if($cnt > 1)
58+
{
59+
while($cnt)
60+
{
61+
break;
62+
}
63+
dec $cnt;
64+
}
65+
66+
--echo cnt=$cnt
67+
68+
# Break in inner if
69+
70+
let $cnt= 4;
71+
while($cnt > 1)
72+
{
73+
if($cnt == 2)
74+
{
75+
break;
76+
--echo "if" is working after break
77+
}
78+
dec $cnt;
79+
}
80+
81+
--echo cnt=$cnt
82+
83+
# Stray break
84+
85+
--error 1
86+
--exec echo "break;" | $MYSQL_TEST 2>&1

0 commit comments

Comments
 (0)