summaryrefslogtreecommitdiff
path: root/test/common/testcommon.cpp
blob: bac26ff3050edc5bce88b7fcf62f8159489f858e (plain)
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
// --------------------------------------------------------------------------
//
// File
//		Name:    testcommon.cpp
//		Purpose: Tests for the code in lib/common
//		Created: 2003/07/23
//
// --------------------------------------------------------------------------

#include "Box.h"

#include <errno.h>
#include <stdio.h>
#include <time.h>

#include "Test.h"
#include "Configuration.h"
#include "FdGetLine.h"
#include "Guards.h"
#include "FileStream.h"
#include "InvisibleTempFileStream.h"
#include "IOStreamGetLine.h"
#include "NamedLock.h"
#include "ReadGatherStream.h"
#include "MemBlockStream.h"
#include "ExcludeList.h"
#include "CommonException.h"
#include "Conversion.h"
#include "autogen_ConversionException.h"
#include "CollectInBufferStream.h"
#include "Archive.h"
#include "Timer.h"

#include "MemLeakFindOn.h"

using namespace BoxConvert;

void test_conversions()
{
	TEST_THAT((Convert<int32_t, const std::string &>(std::string("32"))) == 32);
	TEST_THAT((Convert<int32_t, const char *>("42")) == 42);
	TEST_THAT((Convert<int32_t, const char *>("-42")) == -42);
	TEST_CHECK_THROWS((Convert<int8_t, const char *>("500")), ConversionException, IntOverflowInConvertFromString);
	TEST_CHECK_THROWS((Convert<int8_t, const char *>("pants")), ConversionException, BadStringRepresentationOfInt);
	TEST_CHECK_THROWS((Convert<int8_t, const char *>("")), ConversionException, CannotConvertEmptyStringToInt);
	
	std::string a(Convert<std::string, int32_t>(63));
	TEST_THAT(a == "63");
	std::string b(Convert<std::string, int32_t>(-3473463));
	TEST_THAT(b == "-3473463");
	std::string c(Convert<std::string, int16_t>(344));
	TEST_THAT(c == "344");
}

ConfigurationVerifyKey verifykeys1_1_1[] =
{
	{"bing", 0, ConfigTest_Exists, 0},
	{"carrots", 0, ConfigTest_Exists | ConfigTest_IsInt, 0},
	{"terrible", 0, ConfigTest_Exists | ConfigTest_LastEntry, 0}
};

ConfigurationVerifyKey verifykeys1_1_2[] =
{
	{"fish", 0, ConfigTest_Exists | ConfigTest_IsInt, 0},
	{"string", 0, ConfigTest_Exists | ConfigTest_LastEntry, 0}
};


ConfigurationVerify verifysub1_1[] = 
{
	{
		"*",
		0,
		verifykeys1_1_1,
		ConfigTest_Exists,
		0
	},
	{
		"otherthing",
		0,
		verifykeys1_1_2,
		ConfigTest_Exists | ConfigTest_LastEntry,
		0
	}
};

ConfigurationVerifyKey verifykeys1_1[] =
{
	{"value", 0, ConfigTest_Exists | ConfigTest_IsInt, 0},
	{"string1", 0, ConfigTest_Exists, 0},
	{"string2", 0, ConfigTest_Exists | ConfigTest_LastEntry, 0}
};

ConfigurationVerifyKey verifykeys1_2[] = 
{
	{"carrots", 0, ConfigTest_Exists | ConfigTest_IsInt, 0},
	{"string", 0, ConfigTest_Exists | ConfigTest_LastEntry, 0}
};

ConfigurationVerify verifysub1[] = 
{
	{
		"test1",
		verifysub1_1,
		verifykeys1_1,
		ConfigTest_Exists,
		0
	},
	{
		"ping",
		0,
		verifykeys1_2,
		ConfigTest_Exists | ConfigTest_LastEntry,
		0
	}
};

ConfigurationVerifyKey verifykeys1[] =
{
		{"notExpected", 0, 0, 0},
		{"HasDefaultValue", "Lovely default value", 0, 0},
		{"MultiValue", 0, ConfigTest_MultiValueAllowed, 0},
		{"BoolTrue1", 0, ConfigTest_IsBool, 0},
		{"BoolTrue2", 0, ConfigTest_IsBool, 0},
		{"BoolFalse1", 0, ConfigTest_IsBool, 0},
		{"BoolFalse2", 0, ConfigTest_IsBool, 0},
		{"TOPlevel", 0, ConfigTest_LastEntry | ConfigTest_Exists, 0}
};

ConfigurationVerify verify =
{
	"root",
	verifysub1,
	verifykeys1,
	ConfigTest_Exists | ConfigTest_LastEntry,
	0
};

void safe_sleep(int seconds)
{
	struct timespec ts;
	ts.tv_sec  = seconds;
	ts.tv_nsec = 0;
	while (nanosleep(&ts, &ts) == -1 && errno == EINTR)
	{ /* sleep again */ }
}

int test(int argc, const char *argv[])
{
	// Test self-deleting temporary file streams
	{
		std::string tempfile("testfiles/tempfile");
		TEST_CHECK_THROWS(InvisibleTempFileStream fs(tempfile.c_str()), 
			CommonException, OSFileOpenError);
		InvisibleTempFileStream fs(tempfile.c_str(), O_CREAT);

	#ifdef WIN32
		// file is still visible under Windows
		TEST_THAT(TestFileExists(tempfile.c_str()));

		// opening it again should work
		InvisibleTempFileStream fs2(tempfile.c_str());
		TEST_THAT(TestFileExists(tempfile.c_str()));

		// opening it to create should work
		InvisibleTempFileStream fs3(tempfile.c_str(), O_CREAT);
		TEST_THAT(TestFileExists(tempfile.c_str()));

		// opening it to create exclusively should fail
		TEST_CHECK_THROWS(InvisibleTempFileStream fs4(tempfile.c_str(), 
			O_CREAT | O_EXCL), CommonException, OSFileOpenError);

		fs2.Close();
	#else
		// file is not visible under Unix
		TEST_THAT(!TestFileExists(tempfile.c_str()));

		// opening it again should fail
		TEST_CHECK_THROWS(InvisibleTempFileStream fs2(tempfile.c_str()),
			CommonException, OSFileOpenError);

		// opening it to create should work
		InvisibleTempFileStream fs3(tempfile.c_str(), O_CREAT);
		TEST_THAT(!TestFileExists(tempfile.c_str()));

		// opening it to create exclusively should work
		InvisibleTempFileStream fs4(tempfile.c_str(), O_CREAT | O_EXCL);
		TEST_THAT(!TestFileExists(tempfile.c_str()));

		fs4.Close();
	#endif

		fs.Close();
		fs3.Close();

		// now that it's closed, it should be invisible on all platforms
		TEST_THAT(!TestFileExists(tempfile.c_str()));
	}

	// Test that memory leak detection doesn't crash
	{
		char *test = new char[1024];
		delete [] test;
		MemBlockStream *s = new MemBlockStream(test,12);
		delete s;
	}

#ifdef BOX_MEMORY_LEAK_TESTING
	{
		Timers::Cleanup();

		TEST_THAT(memleakfinder_numleaks() == 0);
		void *block = ::malloc(12);
		TEST_THAT(memleakfinder_numleaks() == 1);
		void *b2 = ::realloc(block, 128*1024);
		TEST_THAT(memleakfinder_numleaks() == 1);
		::free(b2);
		TEST_THAT(memleakfinder_numleaks() == 0);
		char *test = new char[1024];
		TEST_THAT(memleakfinder_numleaks() == 1);
		MemBlockStream *s = new MemBlockStream(test,12);
		TEST_THAT(memleakfinder_numleaks() == 2);
		delete s;
		TEST_THAT(memleakfinder_numleaks() == 1);
		delete [] test;
		TEST_THAT(memleakfinder_numleaks() == 0);

		Timers::Init();
	}
#endif // BOX_MEMORY_LEAK_TESTING

	// test main() initialises timers for us, so uninitialise them
	Timers::Cleanup();
	
	// Check that using timer methods without initialisation
	// throws an exception
	TEST_CHECK_THROWS(Timers::Add(*(Timer*)NULL), 
		CommonException, AssertFailed);
	TEST_CHECK_THROWS(Timers::Remove(*(Timer*)NULL), 
		CommonException, AssertFailed);
	TEST_CHECK_THROWS(Timers::Signal(), CommonException, AssertFailed);
	TEST_CHECK_THROWS(Timers::Cleanup(), CommonException, AssertFailed);
	
	// Check that we can initialise the timers
	Timers::Init();
	
	// Check that double initialisation throws an exception
	TEST_CHECK_THROWS(Timers::Init(), CommonException, AssertFailed);

	// Check that we can clean up the timers
	Timers::Cleanup();
	
	// Check that double cleanup throws an exception
	TEST_CHECK_THROWS(Timers::Cleanup(), CommonException, AssertFailed);

	Timers::Init();

	Timer t0(0); // should never expire
	Timer t1(1);
	Timer t2(2);
	Timer t3(3);
	
	TEST_THAT(!t0.HasExpired());
	TEST_THAT(!t1.HasExpired());
	TEST_THAT(!t2.HasExpired());
	TEST_THAT(!t3.HasExpired());
	
	safe_sleep(1);
	TEST_THAT(!t0.HasExpired());
	TEST_THAT(t1.HasExpired());
	TEST_THAT(!t2.HasExpired());
	TEST_THAT(!t3.HasExpired());
	
	safe_sleep(1);
	TEST_THAT(!t0.HasExpired());
	TEST_THAT(t1.HasExpired());
	TEST_THAT(t2.HasExpired());
	TEST_THAT(!t3.HasExpired());
	
	t1 = Timer(1);
	t2 = Timer(2);
	TEST_THAT(!t0.HasExpired());
	TEST_THAT(!t1.HasExpired());
	TEST_THAT(!t2.HasExpired());
	
	safe_sleep(1);
	TEST_THAT(!t0.HasExpired());
	TEST_THAT(t1.HasExpired());
	TEST_THAT(!t2.HasExpired());
	TEST_THAT(t3.HasExpired());

	// Leave timers initialised for rest of test.
	// Test main() will cleanup after test finishes.

	static char *testfilelines[] =
	{
		"First line",
		"Second line",
		"Third",
		"",
		"",
		"",
		"sdf hjjk",
		"",
		"test",
		"test#not comment",
		"test#not comment",
		"",
		"nice line",
		"fish",
		"",
		"ping",
		"",
		"",
		"Nothing",
		"Nothing",
		0
	};

	// First, test the FdGetLine class -- rather important this works!
	{
		FileHandleGuard<O_RDONLY> file("testfiles"
			DIRECTORY_SEPARATOR "fdgetlinetest.txt");
		FdGetLine getline(file);
		
  		int l = 0;
  		while(testfilelines[l] != 0)
  		{
	  		TEST_THAT(!getline.IsEOF());
	  		std::string line = getline.GetLine(true);
	  		//printf("expected |%s| got |%s|\n", lines[l], line.c_str());
	  		TEST_THAT(strcmp(testfilelines[l], line.c_str()) == 0);
	  		l++;
  		}
  		TEST_THAT(getline.IsEOF());
  		TEST_CHECK_THROWS(getline.GetLine(true), CommonException, GetLineEOF);
	}
	// and again without pre-processing
	{
		FileHandleGuard<O_RDONLY> file("testfiles"
			DIRECTORY_SEPARATOR "fdgetlinetest.txt");
		FILE *file2 = fopen("testfiles" DIRECTORY_SEPARATOR 
			"fdgetlinetest.txt", "r");
		TEST_THAT_ABORTONFAIL(file2 != 0);
		FdGetLine getline(file);
		char ll[512];
		
		while(!feof(file2))
		{
			fgets(ll, sizeof(ll), file2);
			int e = strlen(ll);
			while(e > 0 && (ll[e-1] == '\n' || ll[e-1] == '\r'))
			{
				e--;
			}
			ll[e] = '\0';
		
	  		TEST_THAT(!getline.IsEOF());
	  		std::string line = getline.GetLine(false);
	  		//printf("expected |%s| got |%s|\n", ll, line.c_str());
	  		TEST_THAT(strcmp(ll, line.c_str()) == 0);
		}
  		TEST_THAT(getline.IsEOF());
  		TEST_CHECK_THROWS(getline.GetLine(true), CommonException, GetLineEOF);
		
		fclose(file2);
	}
	
	// Then the IOStream version of get line, seeing as we're here...
	{
		FileStream file("testfiles" DIRECTORY_SEPARATOR 
			"fdgetlinetest.txt", O_RDONLY);
		IOStreamGetLine getline(file);
		
  		int l = 0;
  		while(testfilelines[l] != 0)
  		{
	  		TEST_THAT(!getline.IsEOF());
	  		std::string line;
	  		while(!getline.GetLine(line, true))
	  			;
	  		//printf("expected |%s| got |%s|\n", lines[l], line.c_str());
	  		TEST_THAT(strcmp(testfilelines[l], line.c_str()) == 0);
	  		l++;
  		}
  		TEST_THAT(getline.IsEOF());
  		std::string dummy;
  		TEST_CHECK_THROWS(getline.GetLine(dummy, true), CommonException, GetLineEOF);
	}
	// and again without pre-processing
	{
		FileStream file("testfiles" DIRECTORY_SEPARATOR 
			"fdgetlinetest.txt", O_RDONLY);
		IOStreamGetLine getline(file);

		FILE *file2 = fopen("testfiles" DIRECTORY_SEPARATOR 
			"fdgetlinetest.txt", "r");
		TEST_THAT_ABORTONFAIL(file2 != 0);
		char ll[512];
		
		while(!feof(file2))
		{
			fgets(ll, sizeof(ll), file2);
			int e = strlen(ll);
			while(e > 0 && (ll[e-1] == '\n' || ll[e-1] == '\r'))
			{
				e--;
			}
			ll[e] = '\0';
		
	  		TEST_THAT(!getline.IsEOF());
	  		std::string line;
	  		while(!getline.GetLine(line, false))
	  			;
	  		//printf("expected |%s| got |%s|\n", ll, line.c_str());
	  		TEST_THAT(strcmp(ll, line.c_str()) == 0);
		}
  		TEST_THAT(getline.IsEOF());
  		std::string dummy;
  		TEST_CHECK_THROWS(getline.GetLine(dummy, true), CommonException, GetLineEOF);
		
		fclose(file2);
	}
	
	// Doesn't exist
	{
		std::string errMsg;
		TEST_CHECK_THROWS(std::auto_ptr<Configuration> pconfig(
			Configuration::LoadAndVerify(
				"testfiles" DIRECTORY_SEPARATOR "DOESNTEXIST", 
				&verify, errMsg)), 
			CommonException, OSFileOpenError);
	}

	// Basic configuration test
	{
		std::string errMsg;
		std::auto_ptr<Configuration> pconfig(
			Configuration::LoadAndVerify(
				"testfiles" DIRECTORY_SEPARATOR "config1.txt", 
				&verify, errMsg));
		if(!errMsg.empty())
		{
			printf("UNEXPECTED error msg is:\n------\n%s------\n", errMsg.c_str());
		}
		TEST_THAT_ABORTONFAIL(pconfig.get() != 0);
		TEST_THAT(errMsg.empty());
		TEST_THAT(pconfig->KeyExists("TOPlevel"));
		TEST_THAT(pconfig->GetKeyValue("TOPlevel") == "value");
		TEST_THAT(pconfig->KeyExists("MultiValue"));
		TEST_THAT(pconfig->GetKeyValue("MultiValue") == "single");
		TEST_THAT(!pconfig->KeyExists("not exist"));
		TEST_THAT(pconfig->KeyExists("HasDefaultValue"));
		TEST_THAT(pconfig->GetKeyValue("HasDefaultValue") == "Lovely default value");
		TEST_CHECK_THROWS(pconfig->GetKeyValue("not exist"), CommonException, ConfigNoKey);
		// list of keys
		std::vector<std::string> keylist(pconfig->GetKeyNames());
		TEST_THAT(keylist.size() == 3);
		// will be sorted alphanumerically
		TEST_THAT(keylist[2] == "TOPlevel" && keylist[1] == "MultiValue" && keylist[0] == "HasDefaultValue");
		// list of sub configurations
		std::vector<std::string> sublist(pconfig->GetSubConfigurationNames());
		TEST_THAT(sublist.size() == 2);
		TEST_THAT(sublist[0] == "test1");
		TEST_THAT(sublist[1] == "ping");
		TEST_THAT(pconfig->SubConfigurationExists("test1"));
		TEST_THAT(pconfig->SubConfigurationExists("ping"));
		TEST_CHECK_THROWS(pconfig->GetSubConfiguration("nosubconfig"), CommonException, ConfigNoSubConfig);
		// Get a sub configuration
		const Configuration &sub1 = pconfig->GetSubConfiguration("test1");
		TEST_THAT(sub1.GetKeyValueInt("value") == 12);
		std::vector<std::string> sublist2(sub1.GetSubConfigurationNames());
		TEST_THAT(sublist2.size() == 4);
		// And the sub-sub configs
		const Configuration &sub1_1 = sub1.GetSubConfiguration("subconfig");
		TEST_THAT(sub1_1.GetKeyValueInt("carrots") == 0x2356);
		const Configuration &sub1_2 = sub1.GetSubConfiguration("subconfig2");
		TEST_THAT(sub1_2.GetKeyValueInt("carrots") == -243895);
		const Configuration &sub1_3 = sub1.GetSubConfiguration("subconfig3");
		TEST_THAT(sub1_3.GetKeyValueInt("carrots") == 050);
		TEST_THAT(sub1_3.GetKeyValue("terrible") == "absolutely");
	}	

	static const char *file[] =
	{
		"testfiles" DIRECTORY_SEPARATOR "config2.txt", 
			// Value missing from root
		"testfiles" DIRECTORY_SEPARATOR "config3.txt", 
			// Unexpected {
		"testfiles" DIRECTORY_SEPARATOR "config4.txt", 
			// Missing }
		"testfiles" DIRECTORY_SEPARATOR "config5.txt", 
			// { expected, but wasn't there
		"testfiles" DIRECTORY_SEPARATOR "config6.txt", 
			// Duplicate key
		"testfiles" DIRECTORY_SEPARATOR "config7.txt", 
			// Invalid key (no name)
		"testfiles" DIRECTORY_SEPARATOR "config8.txt", 
			// Not all sub blocks terminated
		"testfiles" DIRECTORY_SEPARATOR "config9.txt", 
			// Not valid integer
		"testfiles" DIRECTORY_SEPARATOR "config9b.txt", 
			// Not valid integer
		"testfiles" DIRECTORY_SEPARATOR "config9c.txt", 
			// Not valid integer
		"testfiles" DIRECTORY_SEPARATOR "config9d.txt", 
			// Not valid integer
		"testfiles" DIRECTORY_SEPARATOR "config10.txt", 
			// Missing key (in subblock)
		"testfiles" DIRECTORY_SEPARATOR "config11.txt", 
			// Unknown key
		"testfiles" DIRECTORY_SEPARATOR "config12.txt", 
			// Missing block
		"testfiles" DIRECTORY_SEPARATOR "config13.txt", 
			// Subconfig (wildcarded) should exist, but missing (ie nothing present)
		"testfiles" DIRECTORY_SEPARATOR "config16.txt", 
			// bad boolean value
		0
	};

	for(int l = 0; file[l] != 0; ++l)	
	{
		std::string errMsg;
		std::auto_ptr<Configuration> pconfig(Configuration::LoadAndVerify(file[l], &verify, errMsg));
		TEST_THAT(pconfig.get() == 0);
		TEST_THAT(!errMsg.empty());
		printf("(%s) Error msg is:\n------\n%s------\n", file[l], errMsg.c_str());
	}
	
	// Check that multivalues happen as expected
	// (single value in a multivalue already checked)
	{
		std::string errMsg;
		std::auto_ptr<Configuration> pconfig(
			Configuration::LoadAndVerify(
				"testfiles" DIRECTORY_SEPARATOR "config14.txt",
			&verify, errMsg));
		TEST_THAT(pconfig.get() != 0);
		TEST_THAT(errMsg.empty());
		TEST_THAT(pconfig->KeyExists("MultiValue"));
		// values are separated by a specific character
		std::string expectedvalue("value1");
		expectedvalue += Configuration::MultiValueSeparator;
		expectedvalue += "secondvalue";
		TEST_THAT(pconfig->GetKeyValue("MultiValue") == expectedvalue);
	}

	// Check boolean values	
	{
		std::string errMsg;
		std::auto_ptr<Configuration> pconfig(
			Configuration::LoadAndVerify(
				"testfiles" DIRECTORY_SEPARATOR "config15.txt",
			&verify, errMsg));
		TEST_THAT(pconfig.get() != 0);
		TEST_THAT(errMsg.empty());
		TEST_THAT(pconfig->GetKeyValueBool("BoolTrue1") == true);
		TEST_THAT(pconfig->GetKeyValueBool("BoolTrue2") == true);
		TEST_THAT(pconfig->GetKeyValueBool("BoolFalse1") == false);
		TEST_THAT(pconfig->GetKeyValueBool("BoolFalse2") == false);
	}
	
	// Test named locks
	{
		NamedLock lock1;
		// Try and get a lock on a name in a directory which doesn't exist
		TEST_CHECK_THROWS(lock1.TryAndGetLock(
				"testfiles" 
				DIRECTORY_SEPARATOR "non-exist"
				DIRECTORY_SEPARATOR "lock"), 
			CommonException, OSFileError);

		// And a more resonable request
		TEST_THAT(lock1.TryAndGetLock(
			"testfiles" DIRECTORY_SEPARATOR "lock1") == true);

		// Try to lock something using the same lock
		TEST_CHECK_THROWS(
			lock1.TryAndGetLock(
				"testfiles" 
				DIRECTORY_SEPARATOR "non-exist"
				DIRECTORY_SEPARATOR "lock2"), 
			CommonException, NamedLockAlreadyLockingSomething);		
#if defined(HAVE_FLOCK) || HAVE_DECL_O_EXLOCK
		// And again on that name
		NamedLock lock2;
		TEST_THAT(lock2.TryAndGetLock(
			"testfiles" DIRECTORY_SEPARATOR "lock1") == false);
#endif
	}
	{
		// Check that it unlocked when it went out of scope
		NamedLock lock3;
		TEST_THAT(lock3.TryAndGetLock(
			"testfiles" DIRECTORY_SEPARATOR "lock1") == true);
	}
	{
		// And unlocking works
		NamedLock lock4;
		TEST_CHECK_THROWS(lock4.ReleaseLock(), CommonException, 
			NamedLockNotHeld);
		TEST_THAT(lock4.TryAndGetLock(
			"testfiles" DIRECTORY_SEPARATOR "lock4") == true);
		lock4.ReleaseLock();
		NamedLock lock5;
		TEST_THAT(lock5.TryAndGetLock(
			"testfiles" DIRECTORY_SEPARATOR "lock4") == true);
		// And can reuse it
		TEST_THAT(lock4.TryAndGetLock(
			"testfiles" DIRECTORY_SEPARATOR "lock5") == true);
	}

	// Test the ReadGatherStream
	{
		#define GATHER_DATA1 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
		#define GATHER_DATA2 "ZYZWVUTSRQPOMNOLKJIHGFEDCBA9876543210zyxwvutsrqpomno"

		// Make two streams
		MemBlockStream s1(GATHER_DATA1, sizeof(GATHER_DATA1));
		MemBlockStream s2(GATHER_DATA2, sizeof(GATHER_DATA2));

		// And a gather stream
		ReadGatherStream gather(false /* no deletion */);
		
		// Add the streams
		int s1_c = gather.AddComponent(&s1);
		int s2_c = gather.AddComponent(&s2);
		TEST_THAT(s1_c == 0);
		TEST_THAT(s2_c == 1);
		
		// Set up some blocks
		gather.AddBlock(s1_c, 11);
		gather.AddBlock(s1_c, 2);
		gather.AddBlock(s1_c, 8, true, 2);
		gather.AddBlock(s2_c, 20);
		gather.AddBlock(s1_c, 20);
		gather.AddBlock(s2_c, 25);
		gather.AddBlock(s1_c, 10, true, 0);
		#define GATHER_RESULT "0123456789abc23456789ZYZWVUTSRQPOMNOLKJIHabcdefghijklmnopqrstGFEDCBA9876543210zyxwvuts0123456789"

		// Read them in...
		char buffer[1024];
		unsigned int r = 0;
		while(r < sizeof(GATHER_RESULT) - 1)
		{
			int s = gather.Read(buffer + r, 7);
			r += s;
			
			TEST_THAT(gather.GetPosition() == r);
			if(r < sizeof(GATHER_RESULT) - 1)
			{
				TEST_THAT(gather.StreamDataLeft());
				TEST_THAT(static_cast<size_t>(gather.BytesLeftToRead()) == sizeof(GATHER_RESULT) - 1 - r);
			}
			else
			{
				TEST_THAT(!gather.StreamDataLeft());
				TEST_THAT(gather.BytesLeftToRead() == 0);
			}
		}
		TEST_THAT(r == sizeof(GATHER_RESULT) - 1);
		TEST_THAT(::memcmp(buffer, GATHER_RESULT, sizeof(GATHER_RESULT) - 1) == 0);
	}
	
	// Test ExcludeList
	{
		ExcludeList elist;
		// Check assumption
		TEST_THAT(Configuration::MultiValueSeparator == '\x01');
		// Add definite entries
		elist.AddDefiniteEntries(std::string("\x01"));
		elist.AddDefiniteEntries(std::string(""));
		elist.AddDefiniteEntries(std::string("Definite1\x01/dir/DefNumberTwo\x01\x01ThingDefThree"));
		elist.AddDefiniteEntries(std::string("AnotherDef"));
		TEST_THAT(elist.SizeOfDefiniteList() == 4);

		// Add regex entries
		#ifdef HAVE_REGEX_H
			elist.AddRegexEntries(std::string("[a-d]+\\.reg$" "\x01" "EXCLUDE" "\x01" "^exclude$"));
			elist.AddRegexEntries(std::string(""));
			TEST_CHECK_THROWS(elist.AddRegexEntries(std::string("[:not_valid")), CommonException, BadRegularExpression);
			TEST_THAT(elist.SizeOfRegexList() == 3);
		#else
			TEST_CHECK_THROWS(elist.AddRegexEntries(std::string("[a-d]+\\.reg$" "\x01" "EXCLUDE" "\x01" "^exclude$")), CommonException, RegexNotSupportedOnThisPlatform);
			TEST_THAT(elist.SizeOfRegexList() == 0);
		#endif

		#ifdef WIN32
		#define CASE_SENSITIVE false
		#else
		#define CASE_SENSITIVE true
		#endif

		// Try some matches!
		TEST_THAT(elist.IsExcluded(std::string("Definite1")) == true);
		TEST_THAT(elist.IsExcluded(std::string("/dir/DefNumberTwo")) == true);
		TEST_THAT(elist.IsExcluded(std::string("ThingDefThree")) == true);
		TEST_THAT(elist.IsExcluded(std::string("AnotherDef")) == true);
		TEST_THAT(elist.IsExcluded(std::string("dir/DefNumberTwo")) == false);

		// Try some case insensitive matches,
		// that should pass on Win32 and fail elsewhere
		TEST_THAT(elist.IsExcluded("DEFINITe1") 
			== !CASE_SENSITIVE);
		TEST_THAT(elist.IsExcluded("/Dir/DefNumberTwo") 
			== !CASE_SENSITIVE);
		TEST_THAT(elist.IsExcluded("thingdefthree") 
			== !CASE_SENSITIVE);

		#ifdef HAVE_REGEX_H
			TEST_THAT(elist.IsExcluded(std::string("b.reg")) == true);
			TEST_THAT(elist.IsExcluded(std::string("B.reg")) == !CASE_SENSITIVE);
			TEST_THAT(elist.IsExcluded(std::string("b.Reg")) == !CASE_SENSITIVE);
			TEST_THAT(elist.IsExcluded(std::string("e.reg")) == false);
			TEST_THAT(elist.IsExcluded(std::string("e.Reg")) == false);
			TEST_THAT(elist.IsExcluded(std::string("DEfinite1")) == !CASE_SENSITIVE);
			TEST_THAT(elist.IsExcluded(std::string("DEXCLUDEfinite1")) == true);
			TEST_THAT(elist.IsExcluded(std::string("DEfinitexclude1")) == !CASE_SENSITIVE);
			TEST_THAT(elist.IsExcluded(std::string("exclude")) == true);
			TEST_THAT(elist.IsExcluded(std::string("ExcludE")) == !CASE_SENSITIVE);
		#endif

		#undef CASE_SENSITIVE
	}

	test_conversions();

	// test that we can use Archive and CollectInBufferStream
	// to read and write arbitrary types to a memory buffer

	{
		CollectInBufferStream buffer;
		ASSERT(buffer.GetPosition() == 0);

		{
			Archive archive(buffer, 0);
			ASSERT(buffer.GetPosition() == 0);

			archive.Write((bool) true);
			archive.Write((bool) false);
			archive.Write((int) 0x12345678);
			archive.Write((int) 0x87654321);
			archive.Write((int64_t)  0x0badfeedcafebabeLL);
			archive.Write((uint64_t) 0xfeedfacedeadf00dLL);
			archive.Write((uint8_t)  0x01);
			archive.Write((uint8_t)  0xfe);
			archive.Write(std::string("hello world!"));
			archive.Write(std::string("goodbye cruel world!"));
		}

		CollectInBufferStream buf2;
		buf2.Write(buffer.GetBuffer(), buffer.GetSize());
		TEST_THAT(buf2.GetPosition() == buffer.GetSize());

		buf2.SetForReading();
		TEST_THAT(buf2.GetPosition() == 0);

		{
			Archive archive(buf2, 0);
			TEST_THAT(buf2.GetPosition() == 0);

			bool b;
			archive.Read(b); TEST_THAT(b == true);
			archive.Read(b); TEST_THAT(b == false);

			int i;
			archive.Read(i); TEST_THAT(i == 0x12345678);
			archive.Read(i); TEST_THAT((unsigned int)i == 0x87654321);

			uint64_t i64;
			archive.Read(i64); TEST_THAT(i64 == 0x0badfeedcafebabeLL);
			archive.Read(i64); TEST_THAT(i64 == 0xfeedfacedeadf00dLL);

			uint8_t i8;
			archive.Read(i8); TEST_THAT(i8 == 0x01);
			archive.Read(i8); TEST_THAT(i8 == 0xfe);

			std::string s;
			archive.Read(s); TEST_THAT(s == "hello world!");
			archive.Read(s); TEST_THAT(s == "goodbye cruel world!");

			TEST_THAT(!buf2.StreamDataLeft());
		}
	}

	return 0;
}