Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / async_read.cs
1 using System;
2 using System.IO;
3 using System.Threading;
4
5 class Test {
6
7         static int sum = 0;
8         static int count = 0;
9         
10         static void async_callback (IAsyncResult ar)
11         {
12                 byte [] buf = (byte [])ar.AsyncState;
13                 Interlocked.Add (ref sum, buf [0]);
14                 Interlocked.Increment (ref count);
15         }
16
17         static int Main () {
18                 byte [] buf = new byte [1];
19                 AsyncCallback ac = new AsyncCallback (async_callback);
20                 IAsyncResult ar;
21                 int sum0 = 0;
22                 int count0 = 0;
23                 
24                 FileStream s = new FileStream ("async_read.exe",  FileMode.Open, FileAccess.Read);
25
26                 s.Position = 0;
27                 while (s.Read (buf, 0, 1) == 1) {
28                         sum0 += buf [0];
29                         count0 ++;
30                 }
31                 
32                 s.Position = 0;
33                 
34                 do {
35                         buf = new byte [1];
36                         ar = s.BeginRead (buf, 0, 1, ac, buf);
37                 } while (s.EndRead (ar) == 1);
38                 
39                 Thread.Sleep (100);
40                 
41                 s.Close ();
42
43                 count0 ++;  // async_callback is invoked for the "finished reading" case too
44                 Console.WriteLine ("CSUM: " + sum + " " + sum0);
45                 Console.WriteLine ("Count: " + count + " " + count0);
46                 if (sum != sum0 || count != count0)
47                         return 1;
48                 
49                 return 0;
50         }
51 }