aboutsummaryrefslogtreecommitdiff
path: root/test/common/span.h
blob: d73a2e226678a4da63b8d3f817803ba519bed1d6 (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
788
789
#include <cxxtest/TestSuite.h>

class SpanTestSuite;

#include "common/span.h"
#include "common/str.h"

class SpanTestSuite : public CxxTest::TestSuite {
	struct Foo {
		int a;
	};

	template <typename ValueType, template <typename> class Derived>
	class SiblingSpanImpl : public Common::SpanImpl<ValueType, Derived> {
		typedef Common::SpanImpl<ValueType, Derived> super_type;
	public:
		COMMON_SPAN_TYPEDEFS
		SiblingSpanImpl() : super_type() {}
		SiblingSpanImpl(pointer data_, size_type size_) : super_type(data_, size_) {}
	};

	template <typename ValueType>
	class SiblingSpan : public SiblingSpanImpl<ValueType, SiblingSpan> {
		typedef SiblingSpanImpl<ValueType, ::SpanTestSuite::SiblingSpan> super_type;
	public:
		COMMON_SPAN_TYPEDEFS
		SiblingSpan() : super_type() {}
		SiblingSpan(pointer data_, size_type size_) : super_type(data_, size_) {}
	};

	template <typename ValueType, template <typename> class Derived>
	class SubSpanImpl : public Common::NamedSpanImpl<ValueType, Derived> {
		typedef Common::NamedSpanImpl<ValueType, Derived> super_type;
	public:
		COMMON_SPAN_TYPEDEFS
		SubSpanImpl() : super_type() {}
		SubSpanImpl(pointer data_,
					size_type size_,
					const Common::String &name_ = Common::String(),
					const size_type sourceByteOffset_ = 0) :
			super_type(data_, size_, name_, sourceByteOffset_) {}

		template <typename Other>
		SubSpanImpl(const Other &other) : super_type(other) {}
	};

	template <typename ValueType>
	class SubSpan : public SubSpanImpl<ValueType, SubSpan> {
		typedef SubSpanImpl<ValueType, ::SpanTestSuite::SubSpan> super_type;
	public:
		COMMON_SPAN_TYPEDEFS
		SubSpan() : super_type() {}
		SubSpan(pointer data_,
				size_type size_,
				const Common::String &name_ = Common::String(),
				const size_type sourceByteOffset_ = 0) :
			super_type(data_, size_, name_, sourceByteOffset_) {}

		template <typename Other>
		SubSpan(const Other &other) : super_type(other) {}
	};

public:
	void test_sibling_span() {
		byte data[] = { 'h', 'e', 'l', 'l', 'o' };
		SiblingSpan<byte> ss(data, sizeof(data));
		Common::Span<byte> superInstance = ss;
		TS_ASSERT_EQUALS(ss.data(), data);
		TS_ASSERT_EQUALS(superInstance.data(), data);
	}

	void test_sub_span() {
		byte data[] = { 'h', 'e', 'l', 'l', 'o' };
		SubSpan<byte> ss(data, sizeof(data), "custom subspan");
		Common::NamedSpan<byte> namedSuper = ss;
		Common::Span<byte> unnamedSuper = ss;
		TS_ASSERT(ss.name() == "custom subspan");
		TS_ASSERT(namedSuper.name() == ss.name());
		TS_ASSERT(unnamedSuper.name() == Common::String::format("%p", (void *)data));
	}

	void test_span_iterator_const() {
		byte data[] = { 'h', 'e', 'l', 'l', 'o' };
		const Common::Span<byte> span(data, sizeof(data));

		Common::Span<byte>::const_iterator it = span.cbegin();

		Common::Span<byte>::const_iterator sameIt(it);

		TS_ASSERT_EQUALS(sameIt, it);

		uint i;
		for (i = 0; it != span.cend(); ++i, ++it) {
			TS_ASSERT_EQUALS(*it, data[i]);
			TS_ASSERT_LESS_THAN(i, sizeof(data));
		}
		TS_ASSERT_EQUALS(i, sizeof(data));

		it = span.cend() - 1;
		for (i = sizeof(data) - 1; it != span.cbegin(); --i, --it) {
			TS_ASSERT_EQUALS(data[i], *it);
		}
		TS_ASSERT_EQUALS(i, 0U);

		it = span.cbegin();

		it += 4;
		TS_ASSERT_EQUALS(data[4], *it);

		it -= 4;
		TS_ASSERT_EQUALS(data[0], *it);

		TS_ASSERT_EQUALS(data[0], *it++);

		TS_ASSERT_EQUALS(data[1], *it--);

		TS_ASSERT_EQUALS(span.cend() - span.cbegin(), 5);

		TS_ASSERT_EQUALS(*(span.cbegin() + 4), data[4]);

		TS_ASSERT_EQUALS(*(span.cend() - 4), data[1]);

		TS_ASSERT(span.cbegin() < span.cend());

		TS_ASSERT(span.cbegin() <= span.cend());
		TS_ASSERT(span.cbegin() <= span.cbegin());

		TS_ASSERT(span.cend() > span.cbegin());

		TS_ASSERT(span.cend() >= span.cbegin());
		TS_ASSERT(span.cend() >= span.cend());
	}

	void test_span_iterator() {
		byte data[] = { 'h', 'e', 'l', 'l', 'o' };
		Common::Span<byte> span(data, sizeof(data));

		// empty iterator should default construct OK
		Common::Span<byte>::iterator defaultIt;

		Common::Span<byte>::iterator it = span.begin();

		Common::Span<byte>::iterator sameIt(it);

		TS_ASSERT_EQUALS(sameIt, it);

		uint i;
		for (i = 0; it != span.end(); ++i, ++it) {
			TS_ASSERT_EQUALS(*it, data[i]);
			TS_ASSERT_LESS_THAN(i, sizeof(data));
		}
		TS_ASSERT_EQUALS(i, sizeof(data));

		it = span.end() - 1;
		for (i = sizeof(data) - 1; it != span.begin(); --i, --it) {
			TS_ASSERT_EQUALS(data[i], *it);
		}
		TS_ASSERT_EQUALS(i, 0U);

		it = span.begin();

		it += 4;
		TS_ASSERT_EQUALS(data[4], *it);

		it -= 4;
		TS_ASSERT_EQUALS(data[0], *it);

		TS_ASSERT_EQUALS(data[0], *it++);

		TS_ASSERT_EQUALS(data[1], *it--);

		TS_ASSERT_EQUALS(span.end() - span.begin(), 5);

		TS_ASSERT_EQUALS(*(span.begin() + 4), data[4]);

		TS_ASSERT_EQUALS(*(span.end() - 4), data[1]);

		TS_ASSERT(span.begin() < span.end());

		TS_ASSERT(span.begin() <= span.end());
		TS_ASSERT(span.begin() <= span.begin());

		TS_ASSERT(span.end() > span.begin());

		TS_ASSERT(span.end() >= span.begin());
		TS_ASSERT(span.end() >= span.end());

		it = span.begin();
		for (i = 0; it != span.end(); ++i, ++it) {
			*it = 'a' + i;
		}

		it = span.begin();
		for (i = 0; it != span.end(); ++i, ++it) {
			TS_ASSERT_EQUALS(*it, 'a' + i);
			TS_ASSERT_EQUALS(data[i], 'a' + i);
		}
	}

	void test_span_iterator_integers() {
		const byte data[] = { 0xFF, 1, 2, 3, 2, 1, 0xFF };
		Common::Span<const byte> span(data, sizeof(data));
		Common::Span<const byte>::const_iterator it = span.cbegin();

		TS_ASSERT_EQUALS(it.getInt8(), -1);
		TS_ASSERT_EQUALS(it.getUint8(), 255);
		TS_ASSERT_EQUALS(it.getInt16BE(), -255);
		TS_ASSERT_EQUALS(it.getUint16BE(), 65281U);
		TS_ASSERT_EQUALS((it + 5).getInt16LE(), -255);
		TS_ASSERT_EQUALS((it + 5).getUint16LE(), 65281U);
		TS_ASSERT_EQUALS(it.getUint24LE(), 131583U);
#ifdef SCUMM_LITTLE_ENDIAN
		TS_ASSERT_EQUALS((it + 3).getUint32(), 4278256131U);
#elif SCUMM_BIG_ENDIAN
		TS_ASSERT_EQUALS(it.getUint32(), 4278256131U);
#else
#error No endianness detected
#endif
		TS_ASSERT_EQUALS(it.getInt32BE(), -16711165);
		TS_ASSERT_EQUALS(it.getUint32BE(), 4278256131U);
		TS_ASSERT_EQUALS((it + 3).getInt32LE(), -16711165);
		TS_ASSERT_EQUALS((it + 3).getUint32LE(), 4278256131U);
	}

	void test_span_iterator_ptr() {
		Foo foo[2];
		foo[0].a = 1;
		foo[1].a = 2;

		const Common::Span<Foo> span(foo, 2);
		Common::Span<Foo>::const_iterator it = span.cbegin();
		TS_ASSERT_EQUALS(it->a, 1);
		++it;
		TS_ASSERT_EQUALS(it->a, 2);

		TS_ASSERT_EQUALS(it[0].a, 2);
		TS_ASSERT_EQUALS(it[-1].a, 1);
		--it;
		TS_ASSERT_EQUALS(it[1].a, 2);
	}

	void test_span_owner() {
		Common::SpanOwner<Common::Span<byte> > owner;
		owner->allocate(3);
		owner[0] = 'a';
		owner[1] = 'b';
		owner[2] = 'c';
		for (int i = 0; i < 3; ++i) {
			TS_ASSERT_EQUALS(owner->getUint8At(i), 'a' + i);
			TS_ASSERT_EQUALS((*owner)[i], 'a' + i);
		}

		{
			Common::SpanOwner<Common::NamedSpan<byte> > owner2;
			TS_ASSERT(owner2->data() == nullptr);
			owner2->allocateFromSpan(*owner);
			TS_ASSERT(owner2->data() != nullptr);
			TS_ASSERT_DIFFERS(owner->data(), owner2->data());

			for (int i = 0; i < 3; ++i) {
				TS_ASSERT_EQUALS(owner2->getUint8At(i), 'a' + i);
				TS_ASSERT_EQUALS((*owner2)[i], 'a' + i);
			}

			TS_ASSERT_EQUALS((bool)owner2, true);
			owner2.release();
			TS_ASSERT_EQUALS((bool)owner2, false);
		}

		{
			Common::SpanOwner<Common::Span<byte> > owner2;
			TS_ASSERT_EQUALS((bool)owner, true);
			void *dataPtr = owner->data();
			owner2 = owner;
			TS_ASSERT_EQUALS((bool)owner, false);
			TS_ASSERT(owner->data() == nullptr);
			TS_ASSERT_EQUALS(owner2->data(), dataPtr);

			// tests destruction of held pointer by reassignment
			owner2 = owner;

			// tests nullipotence of assignment to self
			dataPtr = owner2->data();
			owner2 = owner2;
			TS_ASSERT(owner2->data() == dataPtr);
		}

		{
			char *data = new char[6];
			Common::strlcpy(data, "hello", 6);
			const Common::SpanOwner<Common::Span<const char> > constOwner(Common::Span<const char>(data, 6));
			TS_ASSERT_EQUALS((*constOwner)[0], 'h');
			TS_ASSERT_EQUALS(constOwner->getUint8At(1), 'e');
			TS_ASSERT_EQUALS(constOwner[2], 'l');
		}

		{
			TS_ASSERT_EQUALS((bool)owner, false);
			Common::SpanOwner<Common::Span<byte> > owner2(owner);
			TS_ASSERT_EQUALS((bool)owner2, false);
		}

		{
			owner->allocate(1);
			TS_ASSERT_EQUALS((bool)owner, true);
			Common::SpanOwner<Common::Span<byte> > owner2(owner);
			TS_ASSERT_EQUALS((bool)owner2, true);
			TS_ASSERT_DIFFERS(owner->data(), owner2->data());
		}

		{
			TS_ASSERT_EQUALS((bool)owner, true);
			void *dataPtr = owner->data();
			TS_ASSERT_EQUALS(owner.release(), dataPtr);
			TS_ASSERT_EQUALS((bool)owner, false);
		}
	}

	void test_span_owner_named_span() {
		Common::SpanOwner<Common::NamedSpan<byte> > owner;
		owner->allocate(3, "foo");
		owner[0] = 'a';
		owner[1] = 'b';
		owner[2] = 'c';
		for (int i = 0; i < 3; ++i) {
			TS_ASSERT_EQUALS(owner->getUint8At(i), 'a' + i);
			TS_ASSERT_EQUALS((*owner)[i], 'a' + i);
		}
		TS_ASSERT(owner->name() == "foo");

		{
			Common::SpanOwner<Common::NamedSpan<byte> > owner2;
			TS_ASSERT(owner2->data() == nullptr);
			owner2->allocateFromSpan(*owner);
			TS_ASSERT(owner2->data() != nullptr);
			TS_ASSERT_DIFFERS(owner->data(), owner2->data());
			TS_ASSERT(owner2->name() == "foo");

			for (int i = 0; i < 3; ++i) {
				TS_ASSERT_EQUALS(owner2->getUint8At(i), 'a' + i);
				TS_ASSERT_EQUALS((*owner2)[i], 'a' + i);
			}

			TS_ASSERT_EQUALS((bool)owner2, true);
			owner2.release();
			TS_ASSERT_EQUALS((bool)owner2, false);
		}

		{
			Common::SpanOwner<Common::NamedSpan<byte> > owner2;
			TS_ASSERT_EQUALS((bool)owner, true);
			void *dataPtr = owner->data();
			owner2 = owner;
			TS_ASSERT_EQUALS((bool)owner, false);
			TS_ASSERT(owner->data() == nullptr);
			TS_ASSERT_EQUALS(owner2->data(), dataPtr);

			// tests destruction of held pointer by reassignment
			owner2 = owner;
		}

		{
			char *data = new char[6];
			Common::strlcpy(data, "hello", 6);
			const Common::SpanOwner<Common::NamedSpan<const char> > constOwner(Common::NamedSpan<const char>(data, 6));
			TS_ASSERT_EQUALS((*constOwner)[0], 'h');
			TS_ASSERT_EQUALS(constOwner->getUint8At(1), 'e');
			TS_ASSERT_EQUALS(constOwner[2], 'l');
		}

		{
			TS_ASSERT_EQUALS((bool)owner, false);
			Common::SpanOwner<Common::NamedSpan<byte> > owner2(owner);
			TS_ASSERT_EQUALS((bool)owner2, false);
		}

		{
			owner->allocate(1);
			TS_ASSERT_EQUALS((bool)owner, true);
			Common::SpanOwner<Common::NamedSpan<byte> > owner2(owner);
			TS_ASSERT_EQUALS((bool)owner2, true);
			TS_ASSERT_DIFFERS(owner->data(), owner2->data());
		}

		{
			TS_ASSERT_EQUALS((bool)owner, true);
			void *dataPtr = owner->data();
			TS_ASSERT_EQUALS(owner.release(), dataPtr);
			TS_ASSERT_EQUALS((bool)owner, false);
		}
	}

	void test_span_allocate_from_stream() {
		byte data[] = "hello";
		Common::MemoryReadStream stream(data, sizeof(data));
		Common::SpanOwner<Common::Span<byte> > owner;
		owner->allocateFromStream(stream, 2);
		TS_ASSERT(owner->data() != data);
		TS_ASSERT_EQUALS(owner->size(), 2U);
		TS_ASSERT_EQUALS(owner[0], 'h');
		TS_ASSERT_EQUALS(owner[1], 'e');
		owner.clear();
		TS_ASSERT(owner->data() == nullptr);
		stream.seek(0, SEEK_SET);

		owner->allocateFromStream(stream);
		TS_ASSERT(owner->data() != data);
		TS_ASSERT_EQUALS(owner->size(), sizeof(data));
		TS_ASSERT_EQUALS(owner[0], 'h');
		TS_ASSERT_EQUALS(owner[1], 'e');
		TS_ASSERT_EQUALS(owner[2], 'l');
		TS_ASSERT_EQUALS(owner[3], 'l');
		TS_ASSERT_EQUALS(owner[4], 'o');

		Common::SpanOwner<Common::NamedSpan<const byte> > owner2;
		stream.seek(0, SEEK_SET);
		owner2->allocateFromStream(stream, Common::kSpanMaxSize, "streamname");
		TS_ASSERT(owner2->data() != data);
		TS_ASSERT_EQUALS(owner2->size(), sizeof(data));
		TS_ASSERT_EQUALS(owner2[0], 'h');
		TS_ASSERT_EQUALS(owner2[1], 'e');
		TS_ASSERT_EQUALS(owner2[2], 'l');
		TS_ASSERT_EQUALS(owner2[3], 'l');
		TS_ASSERT_EQUALS(owner2[4], 'o');
		TS_ASSERT_EQUALS(owner2->name(), "streamname");
	}

	void test_span_byte() {
		{
			byte data[] = { 'h', 'e', 'l', 'l', 'o' };
			Common::Span<byte> span(data, sizeof(data));

			TS_ASSERT_EQUALS(span.size(), sizeof(data));
			TS_ASSERT_EQUALS(span.byteSize(), sizeof(data));

			Common::Span<byte> other(span);
			TS_ASSERT_EQUALS(span, other);
			other.clear();
			TS_ASSERT(span != other);

			TS_ASSERT_EQUALS(span[0], 'h');
			TS_ASSERT_EQUALS(span[1], 'e');
			span[1] = 'o';
			TS_ASSERT_EQUALS(span[1], 'o');

			TS_ASSERT((bool)span);
			span.clear();
			TS_ASSERT(!(bool)span);
		}

		{
			byte data[] = { 'h', 'e', 'l', 'l', 'o' };
			const Common::Span<const byte> span(data, sizeof(data));

			TS_ASSERT_EQUALS(span.size(), sizeof(data));
			TS_ASSERT_EQUALS(span.byteSize(), sizeof(data));

			const Common::Span<const byte> other(span);
			TS_ASSERT_EQUALS(span, other);

			TS_ASSERT_EQUALS(span[0], 'h');
			TS_ASSERT_EQUALS(span[1], 'e');
		}
	}

	void test_span_integers() {
		const byte data[] = { 0xFF, 1, 2, 3, 2, 1, 0xFF };
		Common::Span<const byte> span(data, sizeof(data));

		TS_ASSERT_EQUALS(span[0], 255);
		TS_ASSERT_EQUALS(span.getInt8At(0), -1);
		TS_ASSERT_EQUALS(span.getUint8At(0), 255U);
		TS_ASSERT_EQUALS(span.getInt16BEAt(0), -255);
		TS_ASSERT_EQUALS(span.getUint16BEAt(0), 65281U);
		TS_ASSERT_EQUALS(span.getInt16LEAt(5), -255);
		TS_ASSERT_EQUALS(span.getUint16LEAt(5), 65281U);
		TS_ASSERT_EQUALS(span.getUint24LEAt(0), 131583U);
		TS_ASSERT_EQUALS(span.getInt32BEAt(0), -16711165);
		TS_ASSERT_EQUALS(span.getUint32BEAt(0), 4278256131U);
		TS_ASSERT_EQUALS(span.getInt32LEAt(3), -16711165);
		TS_ASSERT_EQUALS(span.getUint32LEAt(3), 4278256131U);

#ifdef SCUMM_LITTLE_ENDIAN
		TS_ASSERT_EQUALS(span.getUint32At(3), 4278256131U);
#elif SCUMM_BIG_ENDIAN
		TS_ASSERT_EQUALS(span.getUint32At(0), 4278256131U);
#else
#error No endianness detected
#endif
	}

	void test_span_string() {
		char data[] = "hello";
		Common::Span<char> span(data, sizeof(data));
		TS_ASSERT_EQUALS(span[sizeof(data) - 1], '\0');

		TS_ASSERT(span.getStringAt(0) == data);
		TS_ASSERT(span.getStringAt(0, 2) == "he");
		span[3] = '\0';
		TS_ASSERT(span.getStringAt(0) == "hel");
	}

	void test_span_unsafe_data() {
		char data[] = "hello";
		Common::Span<char> span(data, sizeof(data));

		char *ptr = span.getUnsafeDataAt(0, 6);
		TS_ASSERT_EQUALS(ptr, data);
		ptr = span.getUnsafeDataAt(0);
		TS_ASSERT_EQUALS(ptr, data);

		const Common::Span<const char> span2(data, sizeof(data));
		const char *ptr2 = span2.getUnsafeDataAt(0, 6);
		TS_ASSERT_EQUALS(ptr2, data);
		ptr2 = span2.getUnsafeDataAt(0);
		TS_ASSERT_EQUALS(ptr2, data);
	}

	void test_span_subspan() {
		{
			byte data[] = { 1, 2, 3, 4, 5, 6 };
			Common::Span<byte> span(data, sizeof(data));

			TS_ASSERT_EQUALS(span.subspan(0).size(), sizeof(data) - 0);
			TS_ASSERT_EQUALS(span.subspan(2).size(), sizeof(data) - 2);
			TS_ASSERT_EQUALS(span.subspan(2, 2).size(), 2U);
			TS_ASSERT_EQUALS(span.subspan<uint16>(0).size(), sizeof(data) / 2);
			TS_ASSERT_EQUALS(span.subspan<uint16>(0).byteSize(), sizeof(data));
			TS_ASSERT_EQUALS(span.subspan<uint16>(0, 2).size(), 1U);
			TS_ASSERT_EQUALS(span.subspan<uint16>(0, 2).byteSize(), 2U);

#ifdef SCUMM_LITTLE_ENDIAN
			TS_ASSERT_EQUALS(span.subspan<uint16>(0)[1], 4 << 8 | 3);
#elif SCUMM_BIG_ENDIAN
			TS_ASSERT_EQUALS(span.subspan<uint16>(0)[1], 3 << 8 | 4);
#else
#error No endianness detected
#endif

			Common::Span<uint16> shortSpan = span.subspan<uint16>(0);
			TS_ASSERT_EQUALS(shortSpan.byteSize(), span.byteSize());
			TS_ASSERT(shortSpan.size() != span.size());
			shortSpan[1] = 0xFFFF;
			Common::Span<byte> byteSpan = shortSpan.subspan<byte>(1);
			TS_ASSERT_EQUALS(byteSpan.size(), sizeof(data) - 1 * sizeof(uint16));
			TS_ASSERT_EQUALS(byteSpan[0], 0xFF);
			TS_ASSERT_EQUALS(byteSpan[1], 0xFF);
		}

		{
			byte data[] = { 1, 2, 3, 4, 5, 6 };
			const Common::Span<const byte> span(data, sizeof(data));

			TS_ASSERT_EQUALS(span.subspan(0).size(), sizeof(data) - 0);
			TS_ASSERT_EQUALS(span.subspan(2).size(), sizeof(data) - 2);
			TS_ASSERT_EQUALS(span.subspan(2, 2).size(), 2U);
			TS_ASSERT_EQUALS(span.subspan<uint16>(0).size(), sizeof(data) / 2);
			TS_ASSERT_EQUALS(span.subspan<uint16>(0).byteSize(), sizeof(data));
			TS_ASSERT_EQUALS(span.subspan<uint16>(0, 2).size(), 1U);
			TS_ASSERT_EQUALS(span.subspan<uint16>(0, 2).byteSize(), 2U);

#ifdef SCUMM_LITTLE_ENDIAN
			TS_ASSERT_EQUALS(span.subspan<uint16>(0)[1], 4 << 8 | 3);
#elif SCUMM_BIG_ENDIAN
			TS_ASSERT_EQUALS(span.subspan<uint16>(0)[1], 3 << 8 | 4);
#else
#error No endianness detected
#endif

			const Common::Span<uint16> shortSpan = span.subspan<uint16>(0);
			TS_ASSERT_EQUALS(shortSpan.byteSize(), span.byteSize());
			TS_ASSERT(shortSpan.size() != span.size());
			Common::Span<byte> byteSpan = shortSpan.subspan<byte>(1);
			TS_ASSERT_EQUALS(byteSpan.size(), sizeof(data) - 1 * sizeof(uint16));
			TS_ASSERT_EQUALS(byteSpan[0], 3);
			TS_ASSERT_EQUALS(byteSpan[1], 4);
		}
	}

	void test_span_to_stream() {
		const byte data[] = { 0, 1, 2, 3 };
		Common::Span<const byte> span(data, sizeof(data));

		{
			Common::MemoryReadStream stream(span.toStream(1, 2));
			byte out;
			TS_ASSERT_EQUALS(stream.read(&out, 1), 1U);
			TS_ASSERT_EQUALS(out, 1);
			TS_ASSERT_EQUALS(stream.read(&out, 1), 1U);
			TS_ASSERT_EQUALS(out, 2);
			TS_ASSERT_EQUALS(stream.read(&out, 1), 0U);
		}

		{
			Common::MemoryReadStream stream = span.toStream();
			byte out;
			TS_ASSERT_EQUALS(stream.read(&out, 1), 1U);
			TS_ASSERT_EQUALS(out, 0);
			TS_ASSERT_EQUALS(stream.read(&out, 1), 1U);
			TS_ASSERT_EQUALS(out, 1);
			TS_ASSERT_EQUALS(stream.read(&out, 1), 1U);
			TS_ASSERT_EQUALS(out, 2);
			TS_ASSERT_EQUALS(stream.read(&out, 1), 1U);
			TS_ASSERT_EQUALS(out, 3);
			TS_ASSERT_EQUALS(stream.read(&out, 1), 0U);
		}
	}

	void test_span_copying() {
		const byte data[] = { 0, 1, 2, 3, 4, 5 };
		Common::Span<const byte> span(data, sizeof(data));

		byte targetData[sizeof(data)] = {};
		Common::Span<byte> target(targetData, sizeof(targetData));
		span.copyDataTo(target);
		for (uint i = 0; i < sizeof(data); ++i) {
			TS_ASSERT_EQUALS(target[i], i);
		}

		byte out[sizeof(data)];
		span.unsafeCopyDataTo(out);
		for (uint i = 0; i < sizeof(data); ++i) {
			TS_ASSERT_EQUALS(out[i], i);
		}
	}

	void test_span_validation() {
		byte data[6];
		Common::Span<byte> span(data, sizeof(data));
		TS_ASSERT(!span.checkInvalidBounds(0, 0));
		TS_ASSERT(!span.checkInvalidBounds(0, 6));
		TS_ASSERT(!span.checkInvalidBounds(2, 4));
		TS_ASSERT(!span.checkInvalidBounds(4, 2));
		TS_ASSERT(!span.checkInvalidBounds(6, 0));
		TS_ASSERT(!span.checkInvalidBounds(2, -2));
		TS_ASSERT(span.checkInvalidBounds(-2, 2)); // negative index disallowed
		TS_ASSERT(span.checkInvalidBounds(6, 1)); // combined positive overflow (+7)
		TS_ASSERT(span.checkInvalidBounds(2, -4)); // negative overflow (-2)
		TS_ASSERT(span.checkInvalidBounds(0, 10)); // delta positive overflow

		const ptrdiff_t big = 1L << (8 * sizeof(ptrdiff_t) - 1);
		TS_ASSERT(span.checkInvalidBounds(big, 0));
		TS_ASSERT(span.checkInvalidBounds(0, big));
		TS_ASSERT(span.checkInvalidBounds(big, big));
	}

	void test_span_validation_message() {
		byte data[1];
		Common::Span<byte> span(data, sizeof(data));

		Common::String source = span.name();
		Common::String actual;
		Common::String expected;

		actual = span.getValidationMessage(12, 34, Common::kValidateRead);
		expected = Common::String::format("Access violation reading %s: 12 + 34 > 1", source.c_str());
		TS_ASSERT_EQUALS(actual, expected);

		actual = span.getValidationMessage(23, 45, Common::kValidateWrite);
		expected = Common::String::format("Access violation writing %s: 23 + 45 > 1", source.c_str());
		TS_ASSERT_EQUALS(actual, expected);

		actual = span.getValidationMessage(-34, -56, Common::kValidateSeek);
		expected = Common::String::format("Access violation seeking %s: -34 + -56 > 1", source.c_str());
		TS_ASSERT_EQUALS(actual, expected);
	}

	void test_span_comparators() {
		byte data[2];
		Common::Span<const byte> span0(data, sizeof(data));
		Common::Span<const byte> span1(data, sizeof(data));
		Common::Span<const byte> span2(data, sizeof(data) - 1);
		Common::Span<const byte> span3(data + 1, sizeof(data) - 1);
		Common::Span<const byte> span4(data + 2, sizeof(data) - 2);

		TS_ASSERT(span0 == span1);
		TS_ASSERT(span0 != span2);
		TS_ASSERT(span0 <= span1);
		TS_ASSERT(span0 <= span3);
		TS_ASSERT(span0 < span3);
		TS_ASSERT(span3 < span4);
		TS_ASSERT(span4 > span3);
		TS_ASSERT(span3 > span0);
		TS_ASSERT(span4 >= span4);
		TS_ASSERT(span0 >= span1);

		TS_ASSERT_EQUALS(span1 - span0, 0);
		TS_ASSERT_EQUALS(span3 - span0, 1);
		TS_ASSERT_EQUALS(span4 - span0, 2);
		TS_ASSERT_EQUALS(span0 - span1, 0);
		TS_ASSERT_EQUALS(span0 - span3, -1);
		TS_ASSERT_EQUALS(span0 - span4, -2);
	}

	void test_named_span() {
		byte data[6] = { 0, 1, 2, 3, 4, 5 };
		Common::NamedSpan<byte> span(data, sizeof(data), "foo.data");
		TS_ASSERT_EQUALS(span.name(), "foo.data");

		Common::String actual;
		Common::String expected;

		actual = span.getValidationMessage(12, 34, Common::kValidateRead);
		expected = "Access violation reading foo.data: 12 + 34 > 6 (abs: 12 + 34 > 6)";
		TS_ASSERT_EQUALS(actual, expected);

		{
			Common::NamedSpan<byte> subspan = span.subspan(2);

			expected = "Access violation reading foo.data: 23 + 45 > 4 (abs: 25 + 45 > 6)";
			actual = subspan.getValidationMessage(23, 45, Common::kValidateRead);
			TS_ASSERT_EQUALS(actual, expected);
		}

		{
			Common::NamedSpan<byte> subspan = span.subspan(2, Common::kSpanMaxSize, "new.data");
			expected = "Access violation reading new.data: -34 + -56 > 4 (abs: -32 + -56 > 6)";
			actual = subspan.getValidationMessage(-34, -56, Common::kValidateRead);
			TS_ASSERT_EQUALS(actual, expected);
		}

		{
			Common::NamedSpan<byte> subspan = span.subspan(2, Common::kSpanMaxSize, "new.data", 0);
			expected = "Access violation reading new.data: -34 + -56 > 4 (abs: -34 + -56 > 4)";
			actual = subspan.getValidationMessage(-34, -56, Common::kValidateRead);
			TS_ASSERT_EQUALS(actual, expected);
		}

		Common::NamedSpan<byte> span2;
		span = span2 = span;
		TS_ASSERT_EQUALS(span2, span);
		TS_ASSERT(span2.name() == span.name());
		TS_ASSERT(span2.sourceByteOffset() == span.sourceByteOffset());

		Common::Span<byte> superclassInstance;
		superclassInstance = span;
		TS_ASSERT_EQUALS(span, superclassInstance);

		Common::Span<byte> subclassInstance(superclassInstance);
		TS_ASSERT_EQUALS(subclassInstance, superclassInstance);

		const Common::NamedSpan<const byte> constSpan(span);

		{
			Common::NamedSpan<const byte> subspan = constSpan.subspan(2);

			expected = "Access violation reading foo.data: 23 + 45 > 4 (abs: 25 + 45 > 6)";
			actual = subspan.getValidationMessage(23, 45, Common::kValidateRead);
			TS_ASSERT_EQUALS(actual, expected);
			TS_ASSERT_EQUALS(subspan.sourceByteOffset(), 2U);
		}

		{
			Common::NamedSpan<const byte> subspan = constSpan.subspan(2, Common::kSpanMaxSize, "new.data");
			expected = "Access violation reading new.data: -34 + -56 > 4 (abs: -32 + -56 > 6)";
			actual = subspan.getValidationMessage(-34, -56, Common::kValidateRead);
			TS_ASSERT_EQUALS(actual, expected);
		}

		{
			Common::NamedSpan<const byte> subspan = constSpan.subspan(2, Common::kSpanMaxSize, "new.data", 0);
			expected = "Access violation reading new.data: -34 + -56 > 4 (abs: -34 + -56 > 4)";
			actual = subspan.getValidationMessage(-34, -56, Common::kValidateRead);
			TS_ASSERT_EQUALS(actual, expected);
		}

		{
			Common::NamedSpan<const byte> subspan = constSpan.subspan(2, Common::kSpanMaxSize, "new.data", 0);
			subspan.sourceByteOffset() = 2;
			expected = "Access violation reading new.data: -34 + -56 > 4 (abs: -32 + -56 > 6)";
			actual = subspan.getValidationMessage(-34, -56, Common::kValidateRead);
			TS_ASSERT_EQUALS(actual, expected);
		}

		{
			Common::MemoryReadStream *stream = new Common::MemoryReadStream(data, sizeof(data));
			Common::File file;
			file.open(stream, "test.txt");
			Common::SpanOwner<Common::NamedSpan<const byte> > fileOwner;
			fileOwner->allocateFromStream(file);
			TS_ASSERT_EQUALS(fileOwner->size(), (uint)file.size());
			file.close();
			TS_ASSERT(fileOwner->name() == "test.txt");
			for (uint i = 0; i < fileOwner->size(); ++i) {
				TS_ASSERT_EQUALS(fileOwner->getInt8At(i), data[i]);
			}
		}
	}
};