[amd64] Save missing register
[mono.git] / mcs / class / Mono.Posix / Test / Mono.Unix / UnixMarshalTest.cs
1 // UnixMarshalTests.cs - NUnit2 Test Cases for Mono.Unix.UnixMarshal class
2 //
3 // Authors:
4 //  Jonathan Pryor (jonpryor@vt.edu)
5 //
6 // (c) 2005 Jonathan Pryor
7 //
8
9 using NUnit.Framework;
10 using System;
11 using System.Collections.Generic;
12 using System.IO;
13 using System.Runtime.InteropServices;
14 using System.Text;
15 using Mono.Unix;
16
17 namespace MonoTests.Mono.Unix {
18
19         class RandomEncoding : UTF8Encoding {
20                 public RandomEncoding ()
21                         : base (false, true)
22                 {
23                 }
24
25                 public override int GetMaxByteCount (int value)
26                 {
27                         return value*6;
28                 }
29         }
30
31         [TestFixture, Category ("NotOnWindows")]
32         public class UnixMarshalTest {
33 #if false
34                 public static void Main ()
35                 {
36                         string s = UnixMarshal.GetErrorDescription (Errno.ERANGE);
37                         Console.WriteLine ("ERANGE={0}", s);
38                         s = UnixMarshal.GetErrorDescription ((Errno) 999999);
39                         Console.WriteLine ("Invalid={0}", s);
40                 }
41 #endif
42
43                 [Test]
44                 public void TestStringToHeap ()
45                 {
46                         object[] data = {
47                                 "Hello, world!", true, true,
48                                 "MS Pゴシック", false, true,
49                         };
50
51                         for (int i = 0; i < data.Length; i += 3) {
52                                 string s           = (string) data [i+0];
53                                 bool valid_ascii   = (bool)   data [i+1];
54                                 bool valid_unicode = (bool)   data [i+2];
55
56                                 StringToHeap (s, valid_ascii, valid_unicode);
57                         }
58                 }
59
60                 private static void StringToHeap (string s, bool validAscii, bool validUnicode)
61                 {
62                         StringToHeap (s, Encoding.ASCII, validAscii);
63                         StringToHeap (s, Encoding.UTF7, validUnicode);
64                         StringToHeap (s, Encoding.UTF8, validUnicode);
65                         StringToHeap (s, Encoding.Unicode, validUnicode);
66                         StringToHeap (s, Encoding.BigEndianUnicode, validUnicode);
67                         StringToHeap (s, new RandomEncoding (), validUnicode);
68                 }
69
70                 private static void StringToHeap (string s, Encoding e, bool mustBeEqual)
71                 {
72                         IntPtr p = UnixMarshal.StringToHeap (s, e);
73                         try {
74                                 string _s = UnixMarshal.PtrToString (p, e);
75                                 if (mustBeEqual)
76                                         Assert.AreEqual (s, _s, "#TSTA (" + e.GetType() + ")");
77                         }
78                         finally {
79                                 UnixMarshal.FreeHeap (p);
80                         }
81                 }
82                 
83                 [Test]
84                 public void TestPtrToString ()
85                 {
86                         IntPtr p = UnixMarshal.AllocHeap (1);
87                         Marshal.WriteByte (p, 0);
88                         string s = UnixMarshal.PtrToString (p);
89                         UnixMarshal.FreeHeap (p);
90                 }
91
92                 [Test]
93                 public void TestUtf32PtrToString ()
94                 {
95                         var utf32NativeEndianNoBom = new UTF32Encoding(
96                                 bigEndian: !BitConverter.IsLittleEndian,
97                                 byteOrderMark: false,
98                                 throwOnInvalidCharacters: true
99                         );
100
101                         // assemble a buffer that contains:
102                         // 1. eight garbage bytes
103                         // 2. the native-endian UTF-32 string "Hello, World" without BOM
104                         // 3. four 0 bytes (as a C wide string terminator)
105                         // 4. the native-endian UTF-32 string "broken" without BOM
106                         // 5. eight 0 bytes
107                         // 6. four garbage bytes
108                         var buf = new List<byte>();
109                         for (int i = 0; i < 2; ++i) {
110                                 buf.Add((byte)0x12);
111                                 buf.Add((byte)0x34);
112                                 buf.Add((byte)0x56);
113                                 buf.Add((byte)0x78);
114                         }
115
116                         buf.AddRange(utf32NativeEndianNoBom.GetBytes("Hello, World"));
117
118                         for (int i = 0; i < 4; ++i) {
119                                 buf.Add((byte)0x00);
120                         }
121
122                         buf.AddRange(utf32NativeEndianNoBom.GetBytes("broken"));
123
124                         for (int i = 0; i < 8; ++i) {
125                                 buf.Add((byte)0x00);
126                         }
127
128                         buf.Add((byte)0x12);
129                         buf.Add((byte)0x34);
130                         buf.Add((byte)0x56);
131                         buf.Add((byte)0x78);
132
133                         // get the array version of this
134                         var bufArr = buf.ToArray();
135
136                         // allocate a buffer that will contain this string
137                         IntPtr bufPtr = UnixMarshal.AllocHeap(bufArr.Length);
138                         string returned;
139                         try
140                         {
141                                 // copy it in
142                                 Marshal.Copy(bufArr, 0, bufPtr, bufArr.Length);
143
144                                 // try getting it back
145                                 returned = UnixMarshal.PtrToString(bufPtr + 8, utf32NativeEndianNoBom);
146                         }
147                         finally
148                         {
149                                 UnixMarshal.FreeHeap(bufPtr);
150                         }
151
152                         Assert.AreEqual("Hello, World", returned);
153                 }
154         }
155 }
156