PageRenderTime 24ms CodeModel.GetById 9ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/csharp/li_std_map_runme.cs

#
C# | 246 lines | 184 code | 37 blank | 25 comment | 66 complexity | db567de21f74461f67d2c6a378a7ef7c MD5 | raw file
  1/* -----------------------------------------------------------------------------
  2 * li_std_map_runme.cs
  3 *
  4 * SWIG C# tester for std_map.i
  5 * This class tests all the functionality of the std_map.i wrapper.
  6 * Upon successful testing, the main function doesn't print out anything.
  7 * If any error is found - it will be printed on the screen.
  8 * ----------------------------------------------------------------------------- */
  9
 10using System;
 11using System.Collections.Generic;
 12using li_std_mapNamespace;
 13
 14public class li_std_map_runme {
 15
 16    private static readonly int collectionSize = 20;
 17    private static readonly int midCollection = collectionSize / 2;
 18
 19    public static void Main()
 20    {
 21        // Set up an int int map
 22        StringIntMap simap = new StringIntMap();
 23        for (int i = 0; i < collectionSize; i++)
 24        {
 25            int val = i * 18;
 26            simap.Add(i.ToString(), val);
 27        }
 28
 29        // Count property test
 30        if (simap.Count != collectionSize)
 31            throw new Exception("Count test failed");
 32
 33        // IsReadOnly property test
 34        if (simap.IsReadOnly)
 35            throw new Exception("IsReadOnly test failed");
 36
 37        // Item indexing test
 38        simap["0"] = 200;
 39        if (simap["0"] != 200)
 40            throw new Exception("Item property test failed");
 41        simap["0"] = 0 * 18;
 42
 43        // ContainsKey() test
 44        for (int i = 0; i < collectionSize; i++)
 45        {
 46            if (!simap.ContainsKey(i.ToString()))
 47                throw new Exception("ContainsKey test " + i + " failed");
 48        }
 49
 50        // ContainsKey() test
 51        for (int i = 0; i < collectionSize; i++)
 52        {
 53            if (!simap.Contains(new KeyValuePair<string, int>(i.ToString(), i * 18)))
 54                throw new Exception("Contains test " + i + " failed");
 55        }
 56
 57        // TryGetValue() test
 58        int value;
 59        bool rc = simap.TryGetValue("3", out value);
 60        if (rc != true || value != (3 * 18))
 61            throw new Exception("TryGetValue test 1 failed");
 62
 63        rc = simap.TryGetValue("-1", out value);
 64        if (rc != false)
 65            throw new Exception("TryGetValue test 2 failed");
 66
 67        // Keys and Values test
 68        {
 69            IList<string> keys = new List<string>(simap.Keys);
 70            IList<int> values = new List<int>(simap.Values);
 71            Dictionary<string, int> check = new Dictionary<string, int>();
 72            if (keys.Count != collectionSize)
 73                throw new Exception("Keys count test failed");
 74
 75            if (values.Count != collectionSize)
 76                throw new Exception("Values count test failed");
 77
 78            for (int i = 0; i < keys.Count; i++)
 79            {
 80                if (simap[keys[i]] != values[i])
 81                    throw new Exception("Keys and values test failed for index " + i);
 82                check.Add(keys[i], values[i]);
 83            }
 84
 85            for (int i = 0; i < collectionSize; i++)
 86            {
 87                if (!check.ContainsKey(i.ToString()))
 88                  throw new Exception("Keys and Values ContainsKey test " + i + " failed");
 89            }
 90        }
 91
 92        // Add and Remove test
 93        for (int i = 100; i < 103; i++)
 94        {
 95            simap.Add(i.ToString(), i * 18);
 96            if (!simap.ContainsKey(i.ToString()) || simap[i.ToString()] != (i * 18))
 97                throw new Exception("Add test failed for index " + i);
 98
 99            simap.Remove(i.ToString());
100            if (simap.ContainsKey(i.ToString()))
101                throw new Exception("Remove test failed for index " + i);
102        }
103
104        for (int i = 200; i < 203; i++)
105        {
106            simap.Add(new KeyValuePair<string, int>(i.ToString(), i * 18));
107            if (!simap.ContainsKey(i.ToString()) || simap[i.ToString()] != (i * 18))
108                throw new Exception("Add explicit test failed for index " + i);
109
110            simap.Remove(new KeyValuePair<string, int>(i.ToString(), i * 18));
111            if (simap.ContainsKey(i.ToString()))
112                throw new Exception("Remove explicit test failed for index " + i);
113        }
114
115        // Duplicate key test
116        try
117        {
118            simap.Add("3", 0);
119            throw new Exception("Adding duplicate key test failed");
120        }
121        catch (ArgumentException)
122        {
123        }
124
125        // CopyTo() test
126        {
127            KeyValuePair<string, int>[] outputarray = new KeyValuePair<string, int>[collectionSize];
128            simap.CopyTo(outputarray);
129            foreach (KeyValuePair<string, int> val in outputarray)
130            {
131                if (simap[val.Key] != val.Value)
132                    throw new Exception("CopyTo (1) test failed, index:" + val.Key);
133            }
134        }
135        {
136            KeyValuePair<string, int>[] outputarray = new KeyValuePair<string, int>[midCollection + collectionSize];
137            simap.CopyTo(outputarray, midCollection);
138            for (int i = midCollection; i < midCollection + collectionSize; i++)
139            {
140                KeyValuePair<string, int> val = outputarray[i];
141                if (simap[val.Key] != val.Value)
142                    throw new Exception("CopyTo (2) test failed, index:" + val.Key);
143            }
144        }
145        {
146            KeyValuePair<string, int>[] outputarray = new KeyValuePair<string, int>[collectionSize - 1];
147            try
148            {
149                simap.CopyTo(outputarray);
150                throw new Exception("CopyTo (4) test failed");
151            }
152            catch (ArgumentException)
153            {
154            }
155        }
156
157        // Clear test
158        simap.Clear();
159        if (simap.Count != 0)
160            throw new Exception("Clear test failed");
161
162        // Test wrapped methods
163        for (int i = 1; i <= 5; i++)
164        {
165            simap[i.ToString()] = i;
166        }
167        double avg = li_std_map.valueAverage(simap);
168        if (avg != 3.0)
169            throw new Exception("Wrapped method valueAverage test failed. Got " + avg);
170
171        string keyStringified = li_std_map.stringifyKeys(simap);
172        if (keyStringified != " 1 2 3 4 5")
173            throw new Exception("Wrapped method stringifyKeys test failed. Got " + keyStringified);
174
175        // Test a map with a new complex type (Struct)
176        {
177            IntStructMap ismap = new IntStructMap();
178            for (int i = 0; i < 10; i++)
179            {
180                ismap.Add(i, new Struct(i * 10.1));
181            }
182
183            if (ismap.Count != 10)
184                throw new Exception("Count test on complex type map failed");
185
186            foreach (KeyValuePair<int, Struct> p in ismap)
187            {
188                if ((p.Key * 10.1) != p.Value.num)
189                    throw new Exception("Iteration test on complex type map failed for index " + p.Key);
190            }
191        }
192
193        // Test a map of pointers
194        {
195            IntStructPtrMap ispmap = new IntStructPtrMap();
196            for (int i = 0; i < 10; i++)
197            {
198                ispmap.Add(i, new Struct(i * 10.1));
199            }
200
201            if (ispmap.Count != 10)
202                throw new Exception("Count test on complex type pointer map failed");
203
204            foreach (KeyValuePair<int, Struct> p in ispmap)
205            {
206                if ((p.Key * 10.1) != p.Value.num)
207                    throw new Exception("Iteration test on complex type pointer map failed for index " + p.Key);
208            }
209        }
210        {
211            IntStructConstPtrMap iscpmap = new IntStructConstPtrMap();
212            for (int i = 0; i < 10; i++)
213            {
214                iscpmap.Add(i, new Struct(i * 10.1));
215            }
216
217            if (iscpmap.Count != 10)
218                throw new Exception("Count test on complex type const pointer map failed");
219
220            foreach (KeyValuePair<int, Struct> p in iscpmap)
221            {
222                if ((p.Key * 10.1) != p.Value.num)
223                    throw new Exception("Iteration test on complex type const pointer map failed for index " + p.Key);
224            }
225        }
226
227        // Test complex type as key (Struct)
228        {
229            StructIntMap limap = new StructIntMap();
230            Struct s7 = new Struct(7);
231            Struct s8 = new Struct(8);
232            limap[s7] = 8;
233            if (limap[s7] != 8)
234                throw new Exception("Assignment test on complex key map failed");
235
236            if (!limap.ContainsKey(s7))
237                throw new Exception("Key test (1) on complex key map failed");
238
239            if (limap.ContainsKey(s8))
240                throw new Exception("Key test (2) on complex key map failed");
241        }
242
243        // All done
244    }
245}
246