-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCompressionExtensionsTests.cs
More file actions
158 lines (128 loc) · 5.01 KB
/
Copy pathCompressionExtensionsTests.cs
File metadata and controls
158 lines (128 loc) · 5.01 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
150
151
152
153
154
155
156
157
158
using System.Text;
using PowerCSharp.Extensions.Compression;
using Xunit;
namespace PowerCSharp.Extensions.Tests;
public class CompressionExtensionsTests
{
[Fact]
public void Deflate_ShouldCompressAndDecompressCorrectly()
{
// Arrange
string originalText = "PowerCSharp is a comprehensive library of extensions and utilities for .NET applications. This is a longer text to ensure compression actually reduces the size. " +
"We need enough data to make the compression meaningful and test the round-trip functionality properly. " +
"This should be sufficient to demonstrate that the compression and decompression work correctly.";
byte[] originalBytes = Encoding.UTF8.GetBytes(originalText);
// Act
byte[]? compressed = originalBytes.Deflate();
byte[]? decompressed = compressed?.DeflateDecompress();
string decompressedText = decompressed != null ? Encoding.UTF8.GetString(decompressed) : string.Empty;
// Assert
Assert.NotNull(compressed);
Assert.True(compressed.Length > 0, "Compressed data should not be empty");
Assert.NotNull(decompressed);
Assert.Equal(originalText, decompressedText);
Assert.Equal(originalBytes.Length, decompressed!.Length);
}
[Fact]
public void Deflate_WithNullData_ShouldReturnNull()
{
// Arrange
byte[]? nullData = null;
// Act
byte[]? result = nullData.Deflate();
// Assert
Assert.Null(result);
}
[Fact]
public void DeflateDecompress_WithNullData_ShouldReturnNull()
{
// Arrange
byte[]? nullData = null;
// Act
byte[]? result = nullData.DeflateDecompress();
// Assert
Assert.Null(result);
}
[Fact]
public void GzipCompress_ShouldCompressAndDecompressCorrectly()
{
// Arrange
string originalText = "PowerCSharp is a comprehensive library of extensions and utilities for .NET applications. This is a longer text to ensure compression actually reduces the size. " +
"We need enough data to make the compression meaningful and test the round-trip functionality properly. " +
"This should be sufficient to demonstrate that the compression and decompression work correctly.";
byte[] originalBytes = Encoding.UTF8.GetBytes(originalText);
// Act
byte[]? compressed = originalBytes.GzipCompress();
byte[]? decompressed = compressed?.GzipDecompress();
string decompressedText = decompressed != null ? Encoding.UTF8.GetString(decompressed) : string.Empty;
// Assert
Assert.NotNull(compressed);
Assert.True(compressed.Length > 0, "Compressed data should not be empty");
Assert.NotNull(decompressed);
Assert.Equal(originalText, decompressedText);
Assert.Equal(originalBytes.Length, decompressed!.Length);
}
[Fact]
public void GzipCompress_WithNullData_ShouldReturnNull()
{
// Arrange
byte[]? nullData = null;
// Act
byte[]? result = nullData.GzipCompress();
// Assert
Assert.Null(result);
}
[Fact]
public void GzipDecompress_WithNullData_ShouldReturnNull()
{
// Arrange
byte[]? nullData = null;
// Act
byte[]? result = nullData.GzipDecompress();
// Assert
Assert.Null(result);
}
[Fact]
public void Deflate_ShouldProduceConsistentResults()
{
// Arrange
byte[] originalBytes = Encoding.UTF8.GetBytes("Test data for compression");
// Act
byte[]? compressed1 = originalBytes.Deflate();
byte[]? compressed2 = originalBytes.Deflate();
// Assert
Assert.NotNull(compressed1);
Assert.NotNull(compressed2);
Assert.Equal(compressed1, compressed2);
}
[Fact]
public void GzipCompress_ShouldProduceConsistentResults()
{
// Arrange
byte[] originalBytes = Encoding.UTF8.GetBytes("Test data for compression");
// Act
byte[]? compressed1 = originalBytes.GzipCompress();
byte[]? compressed2 = originalBytes.GzipCompress();
// Assert
Assert.NotNull(compressed1);
Assert.NotNull(compressed2);
Assert.Equal(compressed1, compressed2);
}
[Fact]
public void Compression_ShouldHandleEmptyData()
{
// Arrange
byte[] emptyBytes = Array.Empty<byte>();
// Act & Assert
byte[]? deflateCompressed = emptyBytes.Deflate();
byte[]? deflateDecompressed = deflateCompressed?.DeflateDecompress();
byte[]? gzipCompressed = emptyBytes.GzipCompress();
byte[]? gzipDecompressed = gzipCompressed?.GzipDecompress();
Assert.NotNull(deflateCompressed);
Assert.NotNull(deflateDecompressed);
Assert.Empty(deflateDecompressed);
Assert.NotNull(gzipCompressed);
Assert.NotNull(gzipDecompressed);
Assert.Empty(gzipDecompressed);
}
}