File: cmd-manager-test.c

package info (click to toggle)
planner 0.14.6-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,872 kB
  • ctags: 6,212
  • sloc: ansic: 67,306; sh: 10,242; xml: 3,495; sql: 886; makefile: 762; python: 3
file content (222 lines) | stat: -rw-r--r-- 6,784 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2008 Maurice van der Pot <griffon26@kfk4ever.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include <config.h>
#include <string.h>
#include <stdlib.h>
#include "src/planner-cmd-manager.h"
#include "self-check.h"

typedef struct {
	gchar actions[100];
	gint  next;
} ActionHistory;

typedef struct {
	PlannerCmd     base;

	ActionHistory *action_history;
	gboolean       allocated;
	gchar          id;
} TestCmd;

static gboolean
test_cmd_do (PlannerCmd *cmd_base)
{
	TestCmd           *cmd;
	ActionHistory     *history;

	cmd = (TestCmd*) cmd_base;

	history = cmd->action_history;

	CHECK_BOOLEAN_RESULT (cmd->allocated, TRUE);
	CHECK_INTEGER_RESULT (history->actions[history->next], 0);
	history->actions[history->next] = cmd->id;
	history->next++;

	return TRUE;
}

static void
test_cmd_undo (PlannerCmd *cmd_base)
{
	TestCmd *cmd;
	ActionHistory *history;

	cmd = (TestCmd*) cmd_base;

	history = cmd->action_history;

	CHECK_BOOLEAN_RESULT (cmd->allocated, TRUE);
	CHECK_BOOLEAN_RESULT (history->next > 0, TRUE);
	CHECK_BOOLEAN_RESULT (history->actions[history->next - 1] == cmd->id, TRUE);

	history->actions[--history->next] = 0;
}

static void
test_cmd_free (PlannerCmd *cmd_base)
{
	TestCmd *cmd;

	cmd = (TestCmd*) cmd_base;

	CHECK_BOOLEAN_RESULT (cmd->allocated, TRUE);
	cmd->allocated = FALSE;
}


static PlannerCmd *
test_cmd (PlannerCmdManager *cmd_manager,
	  ActionHistory     *action_history,
	  gchar              cmd_id)
{
	PlannerCmd  *cmd_base;
	TestCmd     *cmd;
	gchar        cmd_name[10];

	CHECK_BOOLEAN_RESULT (cmd_id != 0, TRUE);

        g_snprintf (cmd_name, sizeof(cmd_name), "cmd %c", cmd_id);

	cmd_base = planner_cmd_new (TestCmd,
				    cmd_name,
				    test_cmd_do,
				    test_cmd_undo,
				    test_cmd_free);

	cmd = (TestCmd *) cmd_base;

	cmd->action_history = action_history;
	cmd->allocated = TRUE;
	cmd->id = cmd_id;

	planner_cmd_manager_insert_and_do (cmd_manager,
					   cmd_base);

	return cmd_base;
}

gint
main (gint argc, gchar **argv)
{
	ActionHistory action_history;

        g_type_init ();

	PlannerCmdManager *cmd_manager = planner_cmd_manager_new ();

	/* Initialise the action history */
	memset(action_history.actions, 0, sizeof(action_history.actions));
	action_history.next = 0;

	/* Test adding a bunch of normal commands */
	test_cmd(cmd_manager, &action_history, '1');
	test_cmd(cmd_manager, &action_history, '2');
	test_cmd(cmd_manager, &action_history, '3');
	test_cmd(cmd_manager, &action_history, '4');
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "1234");

	/* Test undo and redo with normal commands */
	planner_cmd_manager_undo (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "123");

	planner_cmd_manager_redo (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "1234");

	planner_cmd_manager_undo (cmd_manager);
	planner_cmd_manager_undo (cmd_manager);
	planner_cmd_manager_undo (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "1");

	planner_cmd_manager_redo (cmd_manager);
	planner_cmd_manager_redo (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "123");

	/* Test if undo and redo stop at the end of the queue */
	planner_cmd_manager_redo (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "1234");
	planner_cmd_manager_redo (cmd_manager);
	planner_cmd_manager_redo (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "1234");

	planner_cmd_manager_undo (cmd_manager);
	planner_cmd_manager_undo (cmd_manager);
	planner_cmd_manager_undo (cmd_manager);
	planner_cmd_manager_undo (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "");
	planner_cmd_manager_undo (cmd_manager);
	planner_cmd_manager_undo (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "");

	/* Test if performing a new action after some undos prevents redoes
	 * from happening */
	planner_cmd_manager_redo (cmd_manager);
	planner_cmd_manager_redo (cmd_manager);
	test_cmd(cmd_manager, &action_history, '3');
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "123");
	planner_cmd_manager_redo (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "123");

	/* Test adding some transactions as well as normal commands */
	planner_cmd_manager_begin_transaction (cmd_manager, "trans 1");
	test_cmd(cmd_manager, &action_history, 'a');
	test_cmd(cmd_manager, &action_history, 'b');
	test_cmd(cmd_manager, &action_history, 'c');
	test_cmd(cmd_manager, &action_history, 'd');
	planner_cmd_manager_end_transaction (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "123abcd");

	planner_cmd_manager_begin_transaction (cmd_manager, "trans 2");
	test_cmd(cmd_manager, &action_history, 'i');
	planner_cmd_manager_end_transaction (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "123abcdi");

	test_cmd(cmd_manager, &action_history, '5');
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "123abcdi5");

	planner_cmd_manager_begin_transaction (cmd_manager, "trans 3");
	test_cmd(cmd_manager, &action_history, 'x');
	test_cmd(cmd_manager, &action_history, 'y');
	planner_cmd_manager_end_transaction (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "123abcdi5xy");

	/* Test undo & redo with a mix of transactions and normal commands */
	planner_cmd_manager_undo (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "123abcdi5");

	planner_cmd_manager_redo (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "123abcdi5xy");

	planner_cmd_manager_undo (cmd_manager);
	planner_cmd_manager_undo (cmd_manager);
	planner_cmd_manager_undo (cmd_manager);
	planner_cmd_manager_undo (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "123");

	planner_cmd_manager_redo (cmd_manager);
	planner_cmd_manager_redo (cmd_manager);
	CHECK_STRING_RESULT (g_strdup(action_history.actions), "123abcdi");

	g_object_unref (cmd_manager);

	return EXIT_SUCCESS;
}