-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCharBufferTests.cs
More file actions
109 lines (87 loc) · 3.16 KB
/
CharBufferTests.cs
File metadata and controls
109 lines (87 loc) · 3.16 KB
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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;
using YellowCounter.FileSystemState.PathRedux;
using Shouldly;
namespace YellowCounter.FileSystemState.Tests.PathRedux
{
[TestClass]
public class CharBufferTests
{
[TestMethod]
public void CharBuffer1()
{
var charBuffer = new CharBuffer(100);
int idx1 = charBuffer.Store("Hello");
int idx2 = charBuffer.Store("World");
charBuffer.Retrieve(idx1).ToString().ShouldBe("Hello");
charBuffer.Retrieve(idx2).ToString().ShouldBe("World");
}
[TestMethod]
public void CharBuffer2()
{
var charBuffer = new CharBuffer(100);
int idx1 = charBuffer.Store("Hello");
int idx2 = charBuffer.Store("World");
charBuffer.Retrieve(new[] { idx1, idx2 }).ToString().ShouldBe("HelloWorld");
}
[TestMethod]
public void CharBufferRealloc()
{
var charBuffer = new CharBuffer(13);
int idx1 = charBuffer.Store("Hello");
int idx2 = charBuffer.Store("World");
var helloSpan = charBuffer.Retrieve(idx1);
var worldSpan = charBuffer.Retrieve(idx2);
charBuffer.Resize(25);
// These spans are still pointing at the old buffer - how does it avoid
// freeing up the memory?
helloSpan.ToString().ShouldBe("Hello");
worldSpan.ToString().ShouldBe("World");
var hello2Span = charBuffer.Retrieve(idx1);
var world2Span = charBuffer.Retrieve(idx2);
hello2Span.ToString().ShouldBe("Hello");
world2Span.ToString().ShouldBe("World");
}
[TestMethod]
public void CharBufferEnumerate()
{
var charBuffer = new CharBuffer(100);
int idx1 = charBuffer.Store("Hello");
int idx2 = charBuffer.Store("World");
var results = new List<string>();
foreach(var item in charBuffer)
{
results.Add(item.Span.ToString());
}
results.ShouldBe(new[] { "Hello", "World" });
}
[TestMethod]
public void CharBufferMaxCapacity()
{
// To store the text "Hello" without expanding, we need 5 chars for Hello,
// 1 char for the null terminator of Hello, and 1 char for the null terminator
// of the overall buffer.
var charBuffer = new CharBuffer(7);
int idx1 = charBuffer.Store("Hello");
idx1.ShouldNotBe(-1);
charBuffer.Capacity.ShouldBe(7);
charBuffer.Retrieve(idx1).ToString().ShouldBe("Hello");
int c = 0;
foreach(var itm in charBuffer)
{
if(c == 0)
{
itm.Pos.ShouldBe(0);
itm.Span.ToString().ShouldBe("Hello");
}
else
{
throw new Exception("Should only have one item");
}
c++;
}
}
}
}