* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System / Test / System.IO.Compression / DeflateStreamTest.cs
1 /* -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 //
3 // DeflateStreamTest.cs - NUnit Test Cases for the System.IO.Compression.DeflateStream class
4 //
5 // Authors:
6 //      Christopher James Lahey  <clahey@ximian.com>
7 //
8 // (C) 2004 Novell, Inc. <http://www.novell.com>
9 // 
10
11 #if NET_2_0
12
13 using NUnit.Framework;
14 using System;
15 using System.IO;
16 using System.IO.Compression;
17
18 namespace MonoTests.System.IO.Compression
19 {
20         [TestFixture]
21         public class DeflateStreamTest : Assertion
22         {
23                 private static void CopyStream (Stream src, Stream dest)
24                 {
25                         byte[] array = new byte[1024];
26                         int bytes_read;
27                         bytes_read = src.Read (array, 0, 1024);
28                         while (bytes_read != 0) {
29                                 dest.Write (array, 0, bytes_read);
30                                 bytes_read = src.Read (array, 0, 1024);
31                         }
32                 }
33
34                 private static bool compare_buffers (byte[] first, byte[] second, int length)
35                 {
36                         if (first.Length < length || second.Length < length) {
37                                 return false;
38                         }
39                         for (int i = 0; i < length; i++) {
40                                 if (first[i] != second[i]) {
41                                         return false;
42                                 }
43                         }
44                         return true;
45                 }\r
46 \r
47                 [Test]\r
48                 [ExpectedException (typeof (ArgumentNullException))]\r
49                 public void Constructor_Null ()\r
50                 {\r
51                         DeflateStream ds = new DeflateStream (null, CompressionMode.Compress);\r
52                 }\r
53 \r
54                 [Test]\r
55                 [ExpectedException (typeof (ArgumentException))]\r
56                 public void Constructor_InvalidCompressionMode ()\r
57                 {\r
58                         DeflateStream ds = new DeflateStream (new MemoryStream (), (CompressionMode)Int32.MinValue);\r
59                 }\r
60 \r
61                 [Test]
62                 public void CheckCompressDecompress () {
63                         byte [] data = new byte[100000];
64                         for (int i = 0; i < 100000; i++) {
65                                 data[i] = (byte) i;
66                         }
67                         MemoryStream dataStream = new MemoryStream (data);
68                         MemoryStream backing = new MemoryStream ();
69                         DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress, true);
70                         CopyStream (dataStream, compressing);
71                         dataStream.Close();
72                         compressing.Close();
73                         backing.Seek (0, SeekOrigin.Begin);
74                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
75                         MemoryStream output = new MemoryStream ();
76                         CopyStream (decompressing, output);
77                         Assert (compare_buffers (data, output.GetBuffer(), (int) output.Length));
78                         decompressing.Close();
79                         output.Close();
80                 }
81
82                 [Test]
83                 public void CheckDecompress () {
84                         byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
85                         MemoryStream backing = new MemoryStream (data);
86                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
87                         StreamReader reader = new StreamReader (decompressing);
88                         AssertEquals (reader.ReadLine (), "Hello");
89                         decompressing.Close();
90                 }
91
92                 [Test]
93                 [ExpectedException (typeof (ArgumentNullException))]
94                 public void CheckNullRead () {
95                         byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
96                         MemoryStream backing = new MemoryStream (data);
97                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
98                         decompressing.Read (null, 0, 20);
99                 }
100
101                 [Test]
102                 [ExpectedException (typeof (InvalidOperationException))]
103                 public void CheckCompressingRead () {
104                         byte [] dummy = new byte[20];
105                         MemoryStream backing = new MemoryStream ();
106                         DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress);
107                         compressing.Read (dummy, 0, 20);
108                 }
109
110                 [Test]
111                 [ExpectedException (typeof (ArgumentException))]
112                 public void CheckRangeRead () {
113                         byte [] data = {0x11, 0x78, 0x89, 0x91, 0xbe, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
114                         byte [] dummy = new byte[20];
115                         MemoryStream backing = new MemoryStream (data);
116                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
117                         decompressing.Read (dummy, 10, 20);
118                 }
119
120                 [Test]
121                 [ExpectedException (typeof (InvalidDataException))]
122                 public void CheckInvalidDataRead () {
123                         byte [] data = {0x11, 0x78, 0x89, 0x91, 0xbe, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
124                         byte [] dummy = new byte[20];
125                         MemoryStream backing = new MemoryStream (data);
126                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
127                         decompressing.Read (dummy, 0, 20);
128                 }
129
130                 [Test]
131                 [ExpectedException (typeof (ObjectDisposedException))]
132                 public void CheckClosedRead () {
133                         byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
134                         byte [] dummy = new byte[20];
135                         MemoryStream backing = new MemoryStream (data);
136                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
137                         decompressing.Close ();
138                         decompressing.Read (dummy, 0, 20);
139                 }
140
141                 [Test]
142                 [ExpectedException (typeof (ObjectDisposedException))]
143                 public void CheckClosedFlush () {
144                         MemoryStream backing = new MemoryStream ();
145                         DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress);
146                         compressing.Close ();
147                         compressing.Flush ();
148                 }
149
150                 [Test]
151                 [ExpectedException (typeof (NotSupportedException))]
152                 public void CheckSeek () {
153                         byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
154                         MemoryStream backing = new MemoryStream (data);
155                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
156                         decompressing.Seek (20, SeekOrigin.Current);
157                 }
158
159                 [Test]
160                 [ExpectedException (typeof (NotSupportedException))]
161                 public void CheckSetLength () {
162                         byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
163                         MemoryStream backing = new MemoryStream (data);
164                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
165                         decompressing.SetLength (20);
166                 }
167
168                 [Test]
169                 public void CheckGetCanSeekProp () {
170                         byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
171                         MemoryStream backing = new MemoryStream (data);
172                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
173                         AssertEquals (false, decompressing.CanSeek);
174                 }
175
176                 [Test]
177                 public void CheckGetCanReadProp () {
178                         byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
179                         MemoryStream backing = new MemoryStream (data);
180                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
181                         AssertEquals (true, decompressing.CanRead);
182                 }
183
184                 [Test]
185                 public void CheckGetCanWriteProp () {
186                         MemoryStream backing = new MemoryStream ();
187                         DeflateStream compressing = new DeflateStream (backing, CompressionMode.Decompress);
188                         AssertEquals (false, compressing.CanWrite);
189                 }
190
191                 [Test]
192                 [ExpectedException (typeof (NotSupportedException))]
193                 public void CheckSetLengthProp () {
194                         byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
195                         MemoryStream backing = new MemoryStream (data);
196                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
197                         decompressing.SetLength (20);
198                 }
199
200                 [Test]
201                 [ExpectedException (typeof (NotSupportedException))]
202                 public void CheckGetLengthProp () {
203                         byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
204                         MemoryStream backing = new MemoryStream (data);
205                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
206                         long length = decompressing.Length;
207                 }
208
209                 [Test]
210                 [ExpectedException (typeof (NotSupportedException))]
211                 public void CheckGetPositionProp () {
212                         byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
213                         MemoryStream backing = new MemoryStream (data);
214                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
215                         long position = decompressing.Position;
216                 }
217         }
218 }
219
220 #endif