Add copyright notices to all source files and put a license in LICENSE.
[versaplex.git] / wvdotnet / t / wvstream.t.cs
blob4fa234d9946054e94dc8b8f87b9fad153297e15a
1 /*
2 * Versaplex:
3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
5 */
6 #include "wvtest.cs.h"
7 using System;
8 using System.Collections.Generic;
9 using Wv.Test;
10 using Wv;
11 using Wv.Extensions;
13 [TestFixture]
14 public class WvStreamTests
16 [Test] public void basics()
18 using (WvStream s = new WvStream())
20 WVPASS(s.ok);
21 s.close();
22 WVFAIL(s.ok);
25 using (WvStream s = new WvStream())
27 int closed_called = 0, closed_called2 = 0;
28 s.onclose += delegate() { closed_called++; };
29 s.onclose += delegate() { closed_called2++; };
30 Exception e1 = new Exception("e1");
31 Exception e2 = new Exception("e2");
32 WVPASS(s.ok);
33 WVPASSEQ(closed_called, 0);
34 WVPASSEQ(closed_called2, 0);
35 s.err = e1;
36 s.err = e2;
37 WVFAIL(s.ok);
38 WVPASS(s.err != null);
39 WVPASS(s.err == e1);
40 WVPASS(s.err.Message == "e1");
41 WVPASSEQ(closed_called, 1);
42 WVPASSEQ(closed_called2, 1);
46 [Test] public void loopback()
48 using (WvLoopback l = new WvLoopback())
50 l.print("bonk\nwonk\nblonk");
51 WVPASSEQ(l.read(5).FromUTF8(), "bonk\n");
52 WVPASSEQ(l.read(2).FromUTF8(), "wo");
53 string got = null, got2 = null;
54 Action d = delegate() {
55 Console.WriteLine("onreadable!");
56 got = l.read(4096).FromUTF8();
57 got2 = l.read(4096).FromUTF8();
59 l.onreadable += d;
60 l.print("bah");
61 WVPASSEQ(got, null);
62 WVPASSEQ(got2, null);
63 WvStream.runonce(0);
64 WVPASSEQ(got, "nk\nblonkbah");
65 WVPASSEQ(got2, "");
66 WvStream.runonce(0);
67 WVPASSEQ(got, "nk\nblonkbah");
68 l.print("hah");
69 WVPASSEQ(got, "nk\nblonkbah");
70 WvStream.runonce(0);
71 WVPASSEQ(got, "hah");
73 l.onwritable += delegate() {
74 l.print("X");
76 l.onreadable -= d;
78 WvStream.runonce(0);
80 d();
81 WVPASSEQ(got, "X");
85 [Test] public void inbufstream()
87 using (WvLoopback l = new WvLoopback())
88 using (WvInBufStream b = new WvInBufStream(l))
90 string s = null;
91 int wcount = 0, rcount = 0;
93 WVPASS(true);
94 b.onwritable += delegate() {
95 Console.WriteLine("writing");
96 wcount++;
97 b.print("X");
98 Console.WriteLine("done writing");
100 WVPASS(true);
101 b.onreadable += delegate() {
102 Console.WriteLine("reading");
103 rcount++;
104 s = b.getline(0, '\n');
105 Console.WriteLine("done reading");
108 WVPASSEQ(wcount, 0);
109 WVPASSEQ(rcount, 0);
110 WVPASSEQ(s, null);
112 WvStream.runonce(0);
113 WVPASS(true);
114 WvStream.runonce(0);
116 WVPASSEQ(wcount, 2);
117 WVPASSEQ(rcount, 1);
118 WVPASSEQ(s, null);
120 wv.print("putstring\n");
121 b.print("string!\n");
122 WvStream.runonce(10000);
124 WVPASSEQ(wcount, 3);
125 WVPASSEQ(rcount, 2);
126 WVPASS(s != null);
127 WVPASSEQ(s, "XXstring!\n");
131 IEnumerable<int> checker(WvFile f)
133 Console.WriteLine("checker!");
134 WVPASSEQ(f.getline(-1, '\n'), "Line 1\n");
135 yield return 0;
136 WVPASSEQ(f.getline(-1, '\n'), "Line 2!!\n");
137 yield return 0;
138 WVPASSEQ(f.getline(-1, '\n'), "Line 3\rwith CR\n");
139 yield return 0;
140 WVPASSEQ(f.getline(-1, '\n'), null);
141 yield return 0;
142 WVPASS(false); // should never reach here
145 [Test] public void filestream()
147 using (WvFile f = new WvFile("testfile.txt", "r"))
149 f.onreadable += checker(f).ToAction();
150 while (f.ok)
152 Console.WriteLine("iter");
153 WvStream.runonce(1000);
156 if (f.err != null)
158 Console.WriteLine(f.err);
159 WVFAIL(true);
163 using (WvFile f = new WvFile("testfile.txt", "r"))
165 int rcount = 0;
166 f.onreadable += delegate() {
167 rcount++;
169 WVPASSEQ(rcount, 0);
170 WvStream.runonce(10000);
171 WVPASSEQ(rcount, 1);
172 WVPASSEQ(f.read(1).FromUTF8(), "L");
173 WVPASSEQ(f.getline(-1, '\r'), "ine 1\nLine 2!!\nLine 3\r");
174 WvStream.runonce(10000);
175 WVPASSEQ(f.read(1024).FromUTF8(), "with CR\n");
176 WVPASSEQ(rcount, 3);
177 WvStream.runonce(0);
178 WVPASSEQ(rcount, 4);
179 WvStream.runonce(0);
180 WVPASSEQ(rcount, 4);