Beispiel #1
0
        public virtual void DoRandom(int iter, bool ignoreCase)
        {
            CharArrayMap <int?>    map  = new CharArrayMap <int?>(TEST_VERSION_CURRENT, 1, ignoreCase);
            HashMap <string, int?> hmap = new HashMap <string, int?>();

            char[] key;
            for (int i = 0; i < iter; i++)
            {
                int len = Random().Next(5);
                key = new char[len];
                for (int j = 0; j < key.Length; j++)
                {
                    key[j] = (char)Random().Next(127);
                }
                string keyStr  = new string(key);
                string hmapKey = ignoreCase ? keyStr.ToLowerInvariant() : keyStr;

                int val = Random().Next();

                object o1 = map.Put(key, val);
                object o2 = hmap.Put(hmapKey, val);
                assertEquals(o1, o2);

                // add it again with the string method
                assertEquals(val, map.Put(keyStr, val));

                assertEquals(val, map.Get(key, 0, key.Length));
                assertEquals(val, map.Get(key));
                assertEquals(val, map.Get(keyStr));

                assertEquals(hmap.Count, map.size());
            }
        }
Beispiel #2
0
        public virtual void TestToString()
        {
            CharArrayMap <int?> cm = new CharArrayMap <int?>(TEST_VERSION_CURRENT, Collections.SingletonMap <string, int?>("test", 1), false);

            assertEquals("[test]", cm.Keys.ToString());
            assertEquals("[1]", cm.Values.ToString());
            assertEquals("[test=1]", cm.EntrySet().ToString());
            assertEquals("{test=1}", cm.ToString());
            cm.Put("test2", 2);
            assertTrue(cm.Keys.ToString().Contains(", "));
            assertTrue(cm.Values.ToString().Contains(", "));
            assertTrue(cm.EntrySet().ToString().Contains(", "));
            assertTrue(cm.ToString().Contains(", "));
        }
        public virtual void TestToString()
        {
            CharArrayMap <int?> cm = new CharArrayMap <int?>(TEST_VERSION_CURRENT, Collections.SingletonMap <object, int?>("test", 1), false);

            assertEquals("[test]", cm.Keys.ToString());
            //assertEquals("[1]", cm.Values.ToString()); // TODO: In .NET it would not be possible to make a generic type override the ToString() method to customize it like this without wrapping the result.
            assertEquals("[test=1]", cm.EntrySet().ToString());
            assertEquals("{test=1}", cm.ToString());
            cm.Put("test2", 2);
            assertTrue(cm.Keys.ToString().Contains(", ")); // NOTE: See the note in the KeySet() method as for why this test fails.
            //assertTrue(cm.Values.ToString().Contains(", ")); // TODO: In .NET it would not be possible to make a generic type override the ToString() method to customize it like this without wrapping the result.
            assertTrue(cm.EntrySet().ToString().Contains(", "));
            assertTrue(cm.ToString().Contains(", "));
        }
Beispiel #4
0
 /// <summary>
 /// Reads a stem dictionary. Each line contains:
 /// <code>word<b>\t</b>stem</code>
 /// (i.e. two tab separated words)
 /// </summary>
 /// <returns> stem dictionary that overrules the stemming algorithm </returns>
 /// <exception cref="IOException"> If there is a low-level I/O error. </exception>
 public static CharArrayMap <string> GetStemDict(TextReader reader, CharArrayMap <string> result)
 {
     try
     {
         string line;
         while ((line = reader.ReadLine()) != null)
         {
             string[] wordstem = line.Split(new char[] { '\t' }, 2, StringSplitOptions.RemoveEmptyEntries);
             result.Put(wordstem[0], wordstem[1]);
         }
     }
     finally
     {
         IOUtils.Close(reader);
     }
     return(result);
 }
Beispiel #5
0
        public void TestModifyOnUnmodifiable()
        {
            CharArrayMap <int?> map = new CharArrayMap <int?>(TEST_VERSION_CURRENT, 2, false);

            map.Put("foo", 1);
            map.Put("bar", 2);
            int size = map.Count;

            assertEquals(2, size);
            assertTrue(map.ContainsKey("foo"));
            assertEquals(1, map.Get("foo"));
            assertTrue(map.ContainsKey("bar"));
            assertEquals(2, map.Get("bar"));

            map = CharArrayMap.UnmodifiableMap(map);
            assertEquals("Map size changed due to unmodifiableMap call", size, map.Count);
            var NOT_IN_MAP = "SirGallahad";

            assertFalse("Test String already exists in map", map.ContainsKey(NOT_IN_MAP));
            assertNull("Test String already exists in map", map.Get(NOT_IN_MAP));

            try
            {
                map.Put(NOT_IN_MAP.ToCharArray(), 3);
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.Count);
            }

            try
            {
                map.Put(NOT_IN_MAP, 3);
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.Count);
            }

            try
            {
                map.Put(new StringBuilder(NOT_IN_MAP), 3);
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.Count);
            }

            #region Added for better .NET support
            try
            {
                map.Add(NOT_IN_MAP, 3);
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.Count);
            }

            try
            {
                map.Add(new KeyValuePair <string, int?>(NOT_IN_MAP, 3));
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.Count);
            }

            try
            {
                map[new StringBuilder(NOT_IN_MAP)] = 3;
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.Count);
            }

            try
            {
#pragma warning disable 612, 618
                map.Remove(new KeyValuePair <string, int?>("foo", 1));
#pragma warning restore 612, 618
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertEquals("Size of unmodifiable map has changed", size, map.Count);
            }
            #endregion

            try
            {
                map.Clear();
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertEquals("Size of unmodifiable map has changed", size, map.size());
            }

            try
            {
                map.EntrySet().Clear();
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertEquals("Size of unmodifiable map has changed", size, map.size());
            }

            try
            {
                map.Keys.Clear();
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertEquals("Size of unmodifiable map has changed", size, map.size());
            }

            try
            {
                map.Put((object)NOT_IN_MAP, 3);
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.size());
            }

            try
            {
                map.PutAll(Collections.SingletonMap <string, int?>(NOT_IN_MAP, 3));
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.size());
            }

            assertTrue(map.ContainsKey("foo"));
            assertEquals(1, map.Get("foo"));
            assertTrue(map.ContainsKey("bar"));
            assertEquals(2, map.Get("bar"));
        }
Beispiel #6
0
 public virtual bool Add(object o)
 {
     return(map.Put(o, PLACEHOLDER) == null);
 }