File: ByteBufferTests.cs

package info (click to toggle)
bless 0.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,960 kB
  • sloc: cs: 15,261; xml: 1,954; sh: 18; makefile: 15; python: 4
file content (360 lines) | stat: -rw-r--r-- 9,207 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// created on 3/8/2005 at 5:02 PM
/*
 *   Copyright (c) 2005, Alexandros Frantzis (alf82 [at] freemail [dot] gr)
 *
 *   This file is part of Bless.
 *
 *   Bless 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.
 *
 *   Bless 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 Bless; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
using NUnit.Framework;
using System;
using System.Threading;

using Bless.Buffers;

namespace BlessTests.Buffers {

[TestFixture]
public class ByteBufferTests {

	[Test]
	public void AppendDoTest() {
		ByteBuffer bb = new ByteBuffer();
		byte[] ba = {1, 3, 5, 7, 13, 17};
		bb.Append(ba, 0, ba.LongLength);

		Assert.AreEqual(6, bb.Size);
		Assert.AreEqual(1, bb[0]);
		Assert.AreEqual(3, bb[1]);
		Assert.AreEqual(5, bb[2]);
		Assert.AreEqual(7, bb[3]);
		Assert.AreEqual(13, bb[4]);
		Assert.AreEqual(17, bb[5]);
	}

	[Test]
	public void AppendUndoTest() {
		ByteBuffer bb = new ByteBuffer();
		byte[] ba = {1, 3, 5, 7};
		byte[] ba1 = {13, 17};
		bb.Append(ba, 0, ba.LongLength);
		bb.Append(ba1, 0, ba1.LongLength);

		Assert.AreEqual(6, bb.Size, "#1");
		Assert.AreEqual(3, bb[1], "#2");
		Assert.AreEqual(17, bb[5], "#3");

		bb.Undo();

		Assert.AreEqual(4, bb.Size, "#4");
		Assert.AreEqual(1, bb[0], "#5");
		Assert.AreEqual(3, bb[1], "#6");
		Assert.AreEqual(5, bb[2], "#7");
		Assert.AreEqual(7, bb[3], "#8");
	}

	[Test]
	public void InsertDoTest() {
		ByteBuffer bb = new ByteBuffer();
		byte[] ba = {1, 3, 17};
		byte[] ba1 = {5, 7, 13};
		bb.Append(ba, 0, ba.LongLength);
		//bb.Display("D1");
		bb.Insert(2, ba1, 0, ba1.LongLength);
		//bb.Display("D2");

		Assert.AreEqual(6, bb.Size, "#1");
		Assert.AreEqual(1, bb[0], "#2");
		Assert.AreEqual(3, bb[1], "#3");
		Assert.AreEqual(5, bb[2], "#4");
		Assert.AreEqual(7, bb[3], "#5");
		Assert.AreEqual(13, bb[4], "#6");
		Assert.AreEqual(17, bb[5], "#7");
	}

	[Test]
	public void InsertUndoTest() {
		ByteBuffer bb = new ByteBuffer();
		byte[] ba = {1, 3, 17};
		byte[] ba1 = {5, 7, 13};
		bb.Append(ba, 0, ba.LongLength);
		//bb.Display("D1");
		bb.Insert(2, ba1, 0, ba1.LongLength);
		//bb.Display("D2");

		Assert.AreEqual(6, bb.Size, "#1");
		Assert.AreEqual(3, bb[1], "#2");
		Assert.AreEqual(17, bb[5], "#3");

		bb.Undo();
		//bb.Display("D3");
		Assert.AreEqual(3, bb.Size, "#4");
		Assert.AreEqual(1, bb[0], "#5");
		Assert.AreEqual(3, bb[1], "#6");
		Assert.AreEqual(17, bb[2], "#7");
	}

	[Test]
	public void DeleteDoTest() {
		ByteBuffer bb = new ByteBuffer();
		byte[] ba = {1, 3, 17};
		byte[] ba1 = {5, 7, 13};
		bb.Append(ba, 0, ba.LongLength);
		//bb.Display("D1");
		bb.Insert(2, ba1, 0, ba1.LongLength);
		//bb.Display("D2");
		bb.Delete(1, 3);

		Assert.AreEqual(3, bb.Size, "#1");
		Assert.AreEqual(1, bb[0], "#2");
		Assert.AreEqual(13, bb[1], "#3");
		Assert.AreEqual(17, bb[2], "#4");
	}

	[Test]
	public void DeleteUndoTest() {
		ByteBuffer bb = new ByteBuffer();
		byte[] ba = {1, 3, 17};
		byte[] ba1 = {5, 7, 13};
		bb.Append(ba, 0, ba.LongLength);
		//bb.Display("D1");
		bb.Insert(2, ba1, 0, ba1.LongLength);
		//bb.Display("D2");
		bb.Delete(1, 3);

		Assert.AreEqual(3, bb.Size, "#1");
		Assert.AreEqual(1, bb[0], "#2");
		Assert.AreEqual(13, bb[1], "#3");
		Assert.AreEqual(17, bb[2], "#4");

		bb.Undo();

		Assert.AreEqual(6, bb.Size, "#5");
		Assert.AreEqual(1, bb[0], "#6");
		Assert.AreEqual(3, bb[1], "#7");
		Assert.AreEqual(5, bb[2], "#8");
		Assert.AreEqual(7, bb[3], "#9");
		Assert.AreEqual(13, bb[4], "#10");
		Assert.AreEqual(17, bb[5], "#11");
	}

	[Test]
	public void FileLoadTest1() {
		ByteBuffer bb = ByteBuffer.FromFile("test1.bin");

		long size = bb.Size;
		int sum = 0;

		for (int i = 0; i < size; i++)
			sum ^= bb[i];

		Assert.AreEqual( 0x88, sum, "XOR Result");
	}

	[Test]
	public void FileLoadTest2() {
		ByteBuffer bb = ByteBuffer.FromFile("test1.bin");
		byte[] ba = {0x41, 0x61};
		bb.Append(ba, 0, ba.LongLength);

		long size = bb.Size;
		int sum = 0;

		for (int i = 0; i < size; i++)
			sum ^= bb[i];

		Assert.AreEqual( 0x88 ^ 0x20, sum);
	}

	[Test]
	public void FileLoadTest3() {
		ByteBuffer bb = ByteBuffer.FromFile("test1.bin");
		byte[] ba = {0x41, 0x61};
		bb.Insert(768, ba, 0, ba.LongLength);

		long size = bb.Size;
		int sum = 0;

		for (int i = 0; i < size; i++)
			sum ^= bb[i];

		Assert.AreEqual( 0x88 ^ 0x20, sum);
	}

	[Test]
	[Ignore("It fails, perhaps because it is run from nunit")]
	public void FileSaveTest1() {
		ByteBuffer bb = ByteBuffer.FromFile("test1.bin");
		byte[] ba = {0x41, 0x61};
		long size1 = bb.Size;

		bb.Insert(768, ba, 0, ba.LongLength);
		Assert.AreEqual(size1 + 2, bb.Size, "#1");

		IAsyncResult ar = bb.BeginSaveAs("test2.bin", null, null);
		ar.AsyncWaitHandle.WaitOne();
		bb.CloseFile();

		ByteBuffer bb1 = ByteBuffer.FromFile("test2.bin");

		Assert.AreEqual(bb.Size, bb1.Size, "#2");
		long size = bb1.Size;

		for (int i = 0; i < size; i++) {
			if (bb1[i] != bb[i])
				Assert.Fail(string.Format("#3 Difference at {0} ({1}!={2})", i, bb1[i], bb[i]));
		}
	}

	[Test]
	public void HasChangedTest()
	{
		ByteBuffer bb = new ByteBuffer();
		byte[] ba = {0x41, 0x61};
		Assert.AreEqual(false, bb.HasChanged, "#0");
		bb.Append(ba, 0, ba.LongLength);
		Assert.AreEqual(true, bb.HasChanged, "#1");
		bb.Undo();
		Assert.AreEqual(false, bb.HasChanged, "#2");
	}

	[Test]
	public void InsertEmptyUndoTest()
	{
		ByteBuffer bb = new ByteBuffer();
		byte[] ba = {0x01, 0x02, 0xfe, 0xff};
		byte[] baEmpty = new byte[0];

		bb.Append(ba, 0, ba.LongLength);
		bb.Insert(2, baEmpty, 0, baEmpty.LongLength);

		Assert.AreEqual(4, bb.Size, "#Size before undo");
		Assert.AreEqual(0x01, bb[0], "#bb[0] before undo");
		Assert.AreEqual(0x02, bb[1], "#bb[1] before undo");
		Assert.AreEqual(0xfe, bb[2], "#bb[2] before undo");
		Assert.AreEqual(0xff, bb[3], "#bb[3] before undo");

		bb.Undo();
		Assert.AreEqual(4, bb.Size, "#Size after undo");
		Assert.AreEqual(0x01, bb[0], "#bb[0] after undo");
		Assert.AreEqual(0x02, bb[1], "#bb[1] after undo");
		Assert.AreEqual(0xfe, bb[2], "#bb[2] after undo");
		Assert.AreEqual(0xff, bb[3], "#bb[3] after undo");
	}

	[Test]
	public void AppendEmptyUndoTest()
	{
		ByteBuffer bb = new ByteBuffer();
		byte[] ba = {0x01, 0x02, 0xfe, 0xff};
		byte[] baEmpty = new byte[0];

		bb.Append(ba, 0, ba.LongLength);
		bb.Append(baEmpty, 0, baEmpty.LongLength);

		Assert.AreEqual(4, bb.Size, "#Size before undo");
		Assert.AreEqual(0x01, bb[0], "#bb[0] before undo");
		Assert.AreEqual(0x02, bb[1], "#bb[1] before undo");
		Assert.AreEqual(0xfe, bb[2], "#bb[2] before undo");
		Assert.AreEqual(0xff, bb[3], "#bb[3] before undo");

		bb.Undo();
		Assert.AreEqual(4, bb.Size, "#Size after undo");
		Assert.AreEqual(0x01, bb[0], "#bb[0] after undo");
		Assert.AreEqual(0x02, bb[1], "#bb[1] after undo");
		Assert.AreEqual(0xfe, bb[2], "#bb[2] after undo");
		Assert.AreEqual(0xff, bb[3], "#bb[3] after undo");
	}

	[Test]
	public void ActionChainingTest()
	{
		ByteBuffer bb = new ByteBuffer();
		byte[] ba1 = {0x01, 0x04};
		byte[] ba2 = {0x02, 0x03};
		byte[] ba3 = {0x05, 0x06};
		byte[] ba4 = {0x07, 0x08};

		bb.BeginActionChaining();
		bb.Append(ba1, 0, ba1.LongLength);
		bb.Insert(1, ba2, 0, ba2.LongLength);
		bb.EndActionChaining();

		Assert.AreEqual(4, bb.Size, "#1");
		Assert.IsTrue(bb.CanUndo, "#2");
		bb.Undo();
		Assert.AreEqual(0, bb.Size, "#3");
		Assert.IsFalse(bb.CanUndo, "#4");

		bb.Redo();

		bb.BeginActionChaining();
		bb.Append(ba4, 0, ba4.LongLength);
		bb.Insert(4, ba3, 0, ba3.LongLength);
		bb.EndActionChaining();

		Assert.AreEqual(8, bb.Size, "#5");
		Assert.IsTrue(bb.CanUndo, "#6");

		bb.Undo();

		Assert.AreEqual(4, bb.Size, "#7");
		Assert.IsTrue(bb.CanUndo, "#8");
	}

	[Test]
	public void LimitedUndoTest()
	{
		ByteBuffer bb = new ByteBuffer();
		bb.MaxUndoActions = 3;

		byte[] ba1 = {0x01, 0x04};
		byte[] ba2 = {0x02, 0x03};
		byte[] ba3 = {0x05, 0x06};
		byte[] ba4 = {0x07, 0x08};


		bb.Append(ba1, 0, ba1.LongLength);
		bb.Insert(1, ba2, 0, ba2.LongLength);
		bb.Append(ba4, 0, ba4.LongLength);
		bb.Insert(4, ba3, 0, ba3.LongLength);

		Assert.AreEqual(true, bb.CanUndo, "#1");
		bb.Undo();
		Assert.AreEqual(true, bb.CanUndo, "#2");
		bb.Undo();
		Assert.AreEqual(true, bb.CanUndo, "#3");
		bb.Undo();
		Assert.AreEqual(false, bb.CanUndo, "#4");
		Assert.AreEqual(2, bb.Size, "#5");
		Assert.AreEqual(0x01, bb[0], "#6");
		Assert.AreEqual(0x04, bb[1], "#7");

		bb.Redo();
		bb.Redo();
		bb.Redo();
		Assert.AreEqual(8, bb.Size, "#8");
		bb.MaxUndoActions = 2;

		bb.Undo();
		Assert.AreEqual(6, bb.Size, "#8.5");
		Assert.AreEqual(true, bb.CanUndo, "#9");
		bb.Undo();
		Assert.AreEqual(false, bb.CanUndo, "#10");
		Assert.AreEqual(4, bb.Size, "#11");
	}
}

} // end namespace