-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLengthPropertyTests.cs
More file actions
149 lines (121 loc) · 4.73 KB
/
LengthPropertyTests.cs
File metadata and controls
149 lines (121 loc) · 4.73 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
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
using System;
using Xunit;
using Platform.Collections;
namespace Platform.Collections.Tests
{
public class LengthPropertyTests
{
[Fact]
public void Length_IncreaseFromZero_ShouldWork()
{
var bitString = new BitString(0);
Assert.Equal(0, bitString.Length);
// Should be able to increase length
bitString.Length = 64;
Assert.Equal(64, bitString.Length);
// All bits should be false initially
for (int i = 0; i < 64; i++)
{
Assert.False(bitString[i]);
}
}
[Fact]
public void Length_IncreaseFromNonZero_ShouldPreserveExistingBits()
{
var bitString = new BitString(32);
// Set some bits
bitString[5] = true;
bitString[15] = true;
bitString[25] = true;
// Increase length
bitString.Length = 64;
Assert.Equal(64, bitString.Length);
// Original bits should be preserved
Assert.True(bitString[5]);
Assert.True(bitString[15]);
Assert.True(bitString[25]);
// New bits should be false
for (int i = 32; i < 64; i++)
{
Assert.False(bitString[i]);
}
}
[Fact]
public void Length_DecreaseLength_ShouldWork()
{
var bitString = new BitString(64);
// Set some bits
bitString[5] = true;
bitString[15] = true;
bitString[25] = true;
bitString[45] = true;
bitString[55] = true;
// Decrease length
bitString.Length = 32;
Assert.Equal(32, bitString.Length);
// Bits within new length should be preserved
Assert.True(bitString[5]);
Assert.True(bitString[15]);
Assert.True(bitString[25]);
// Should not be able to access bits beyond new length
Assert.Throws<ArgumentOutOfRangeException>(() => bitString[45]);
Assert.Throws<ArgumentOutOfRangeException>(() => bitString[55]);
}
[Fact]
public void Length_SetToSameValue_ShouldNotChangeAnything()
{
var bitString = new BitString(32);
bitString[5] = true;
bitString[15] = true;
bitString.Length = 32; // Set to same value
Assert.Equal(32, bitString.Length);
Assert.True(bitString[5]);
Assert.True(bitString[15]);
}
[Fact]
public void Length_CrossWordBoundaries_ShouldWork()
{
var bitString = new BitString(63); // Just under 64 (1 word)
bitString[62] = true;
// Increase to cross word boundary
bitString.Length = 128; // 2 words
Assert.Equal(128, bitString.Length);
Assert.True(bitString[62]);
// Set bit in second word
bitString[100] = true;
Assert.True(bitString[100]);
// Decrease back
bitString.Length = 63;
Assert.Equal(63, bitString.Length);
Assert.True(bitString[62]);
}
[Fact]
public void Length_SetToZero_ShouldWork()
{
var bitString = new BitString(64);
bitString[5] = true;
bitString[15] = true;
bitString.Length = 0;
Assert.Equal(0, bitString.Length);
// Should not be able to access any bits
Assert.Throws<ArgumentOutOfRangeException>(() => bitString[0]);
}
[Fact]
public void Length_HandlePartialWordMasking_ShouldWork()
{
var bitString = new BitString(70); // 1 full word + 6 bits in second word
// Set bits in both words
bitString[63] = true; // Last bit of first word
bitString[64] = true; // First bit of second word
bitString[69] = true; // Last valid bit
// Decrease to middle of second word
bitString.Length = 67; // Should keep first 3 bits of second word
Assert.Equal(67, bitString.Length);
Assert.True(bitString[63]);
Assert.True(bitString[64]);
Assert.Throws<ArgumentOutOfRangeException>(() => bitString[69]);
// The bit at index 69 should be cleared from internal storage
// even though we can't access it anymore
}
}
}