[browser][wasm][tests] Add tests for Map (#38862)
authorKenneth Pouncey <kjpou@pt.lu>
Thu, 9 Jul 2020 22:46:01 +0000 (00:46 +0200)
committerGitHub <noreply@github.com>
Thu, 9 Jul 2020 22:46:01 +0000 (17:46 -0500)
* [browser][wasm][map] Add tests for Map

* Address review comments

src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Map.cs
src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.Tests.csproj
src/libraries/System.Runtime.InteropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript/MapTests.cs [new file with mode: 0644]

index ad1923b..41a9e5f 100644 (file)
@@ -11,12 +11,10 @@ namespace System.Runtime.InteropServices.JavaScript
     /// </summary>
     public class Map : CoreObject, IDictionary
     {
-
         /// <summary>
         /// Initializes a new instance of the Map class.
         /// </summary>
-        /// <param name="_params">Parameters.</param>
-        public Map(params object[] _params) : base(Runtime.New<Map>(_params))
+        public Map() : base(Runtime.New<Map>())
         { }
 
         /// <summary>
@@ -183,10 +181,7 @@ namespace System.Runtime.InteropServices.JavaScript
 
             public object SyncRoot => this;
 
-            public void CopyTo(System.Array array, int index)
-            {
-                throw new NotImplementedException();
-            }
+            public void CopyTo(System.Array array, int index) => throw new NotImplementedException();
 
             // Construct and return an enumerator.
             public IEnumerator GetEnumerator() => new MapItemEnumerator(this);
index e0feae3..68b945e 100644 (file)
@@ -11,6 +11,7 @@
     <Compile Include="System\Runtime\InteropServices\JavaScript\JavaScriptTests.cs" />
     <Compile Include="System\Runtime\InteropServices\JavaScript\DataViewTests.cs" />
     <Compile Include="System\Runtime\InteropServices\JavaScript\TypedArrayTests.cs" />
-    <Compile Include="System\Runtime\InteropServices\JavaScript\ArrayTests.cs" />    
+    <Compile Include="System\Runtime\InteropServices\JavaScript\ArrayTests.cs" />
+    <Compile Include="System\Runtime\InteropServices\JavaScript\MapTests.cs" />
   </ItemGroup>
 </Project>
\ No newline at end of file
diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript/MapTests.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript/MapTests.cs
new file mode 100644 (file)
index 0000000..2cff780
--- /dev/null
@@ -0,0 +1,214 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System.Collections;
+using System.Collections.Generic;
+using System.Runtime.InteropServices.JavaScript;
+using Xunit;
+
+namespace System.Runtime.InteropServices.JavaScript.Tests
+{
+    public static class MapTests
+    {
+        [Fact]
+        public static void Ctor_Empty()
+        {
+            var map = new Map();
+            Assert.Equal(0, map.Count);
+            Assert.Equal(0, map.Length);
+        }
+
+        [Theory]
+        [InlineData(0)]
+        [InlineData(10)]
+        [InlineData(100)]
+        public static void Map_Add(int numberOfElements)
+        {
+            var map = new Map();
+            for (int i = 0; i < numberOfElements; i++)
+            {
+                map.Add(i, $"value{i}");
+            }
+            Assert.Equal(numberOfElements, map.Count);
+        }
+
+        [Theory]
+        [InlineData(0)]
+        [InlineData(10)]
+        [InlineData(1000)]
+        public static void Map_AddContains(int numberOfElements)
+        {
+            var map = new Map();
+            for (int i = 0; i < numberOfElements; i++)
+            {
+                map.Add(i, $"value{i}");
+            }
+            Assert.Equal(numberOfElements, map.Count);
+
+            for (int i = 0; i < numberOfElements; i++)
+            {
+                Assert.True(map.Contains(i));
+            }
+        }
+
+        [Theory]
+        [InlineData(0)]
+        [InlineData(10)]
+        [InlineData(1000)]
+        public static void Map_Iterator(int numberOfElements)
+        {
+            var map = new Map();
+            for (int i = 0; i < numberOfElements; i++)
+            {
+                map.Add(i, $"value{i}");
+            }
+            Assert.Equal(numberOfElements, map.Count);
+
+            int d = 0;
+            foreach (var value in map)
+            {
+                d++;
+            }
+            Assert.Equal(numberOfElements, d);
+        }
+
+        [Theory]
+        [InlineData(0)]
+        [InlineData(10)]
+        [InlineData(1000)]
+        public static void Map_IteratorKeyValue(int numberOfElements)
+        {
+            var map = new Map();
+            for (int i = 0; i < numberOfElements; i++)
+            {
+                map.Add(i, $"value{i}");
+            }
+            Assert.Equal(numberOfElements, map.Count);
+
+            int d = 0;
+            foreach (DictionaryEntry value in map)
+            {
+                Assert.Equal(d, value.Key);
+                Assert.Equal($"value{d++}", value.Value);
+            }
+            Assert.Equal(numberOfElements, d);
+        }
+
+        [Theory]
+        [InlineData(10)]
+        [InlineData(100)]
+        public static void Map_Contains(int numberOfElements)
+        {
+            var map = new Map();
+            for (int i = 0; i < numberOfElements; i++)
+            {
+                map.Add(i, $"value{i}");
+            }
+            Assert.Equal(numberOfElements, map.Count);
+            Assert.True(map.Contains(numberOfElements - 1));
+        }
+
+        [Fact]
+        public static void Add_ClearRepeatedly()
+        {
+            const int Iterations = 2;
+            const int Count = 2;
+
+            var hash = new Map();
+            for (int i = 0; i < Iterations; i++)
+            {
+                for (int j = 0; j < Count; j++)
+                {
+                    string key = $"Key: i={i}, j={j}";
+                    string value = $"Value: i={i}, j={j}";
+                    hash.Add(key, value);
+                }
+
+                Assert.Equal(Count, hash.Count);
+                hash.Clear();
+            }
+        }
+
+        [Fact]
+        public static void ContainsKey()
+        {
+            var map1 = Helpers.CreateStringMap(100);
+            Helpers.PerformActionOnAllMapWrappers(map1, map2 =>
+            {
+                for (int i = 0; i < map2.Count; i++)
+                {
+                    string key = $"Key_{i}";
+                    Assert.True(map2.Contains(key));
+                }
+
+                Assert.False(map2.Contains("Non Existent Key"));
+                Assert.False(map2.Contains(101));
+
+                string removedKey = "Key_1";
+                map2.Remove(removedKey);
+                Assert.False(map2.Contains(removedKey));
+            });
+        }
+
+        [Fact]
+        public static void RemoveKey()
+        {
+            var map1 = Helpers.CreateStringMap(100);
+            Helpers.PerformActionOnAllMapWrappers(map1, map2 =>
+            {
+                for (int i = 0; i < map2.Count; i++)
+                {
+                    string key = $"Key_{i}";
+                    Assert.True(map2.Contains(key));
+                }
+                Assert.Equal(map2.Count, map2.Keys.Count);
+
+                foreach (var key in map2.Keys)
+                {
+                    map2.Remove(key);
+                }
+                Assert.Equal(0, map2.Keys.Count);
+            });
+        }
+
+        [Fact]
+        public static void ContainsValue()
+        {
+            Map map1 = Helpers.CreateStringMap(100);
+            Helpers.PerformActionOnAllMapWrappers(map1, map2 =>
+            {
+                for (int i = 0; i < map2.Count; i++)
+                {
+                    string value = $"Value_{i}";
+                    Assert.True(map2[$"Key_{i}"].ToString() == value);
+                }
+                Assert.True(map2["Non Existent Value"] == null);
+                Assert.True(map2[101] == null);
+            });
+        }
+
+        private static class Helpers
+        {
+            public static void PerformActionOnAllMapWrappers(Map map, Action<Map> action)
+            {
+                action(map);
+            }
+
+            public static Map CreateStringMap(int count, int start = 0)
+            {
+                var map = new Map();
+
+                for (int i = start; i < start + count; i++)
+                {
+                    string key = $"Key_{i}";
+                    string value = $"Value_{i}";
+
+                    map.Add(key, value);
+                }
+
+                return map;
+            }
+        }
+    }
+}