Skip to content

Commit

Permalink
Add support unit tests for ConcurrentHashSet, apache#1117 (apache#1128)
Browse files Browse the repository at this point in the history
* Add support unit tests for ConcurrentHashSet, apache#1117

* Add Harmony Support_ unit tests, apache#1117

* Add IReadOnlySet for .NET 5+
  • Loading branch information
paulirwin authored Feb 15, 2025
1 parent 7401968 commit 6b161d9
Show file tree
Hide file tree
Showing 6 changed files with 795 additions and 23 deletions.
115 changes: 115 additions & 0 deletions src/Lucene.Net.Tests/Support/Support_CollectionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Adapted from Apache Harmony tests via J2N: https://github.com/NightOwl888/J2N/blob/main/tests/NUnit/J2N.Tests/Collections/Support_CollectionTest.cs
using Lucene.Net.Util;
using System.Collections.Generic;
using JCG = J2N.Collections.Generic;

namespace Lucene.Net
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

public class Support_CollectionTest : LuceneTestCase
{
readonly ICollection<int> col; // must contain the Integers 0 to 99

// LUCENENET: removed unused string argument and overload
public Support_CollectionTest(/*String p1,*/ ICollection<int> c)
//: base(p1)
{
col = c;
}

public void RunTest()
{
new Support_UnmodifiableCollectionTest(col).RunTest();

// setup
ICollection<int> myCollection = new JCG.SortedSet<int>();
myCollection.Add(101);
myCollection.Add(102);
myCollection.Add(103);

// add
//assertTrue("CollectionTest - a) add did not work", col.Add(new Integer(
// 101)));
col.Add(101); // Does not return in .NET
assertTrue("CollectionTest - b) add did not work", col
.Contains(101));

// remove
assertTrue("CollectionTest - a) remove did not work", col
.Remove(101));
assertTrue("CollectionTest - b) remove did not work", !col
.Contains(101));

if (col is ISet<int> set)
{
// addAll
//assertTrue("CollectionTest - a) addAll failed", set
// .UnionWith(myCollection));
set.UnionWith(myCollection); // Does not return in .NET
assertTrue("CollectionTest - b) addAll failed", set
.IsSupersetOf(myCollection));

// containsAll
assertTrue("CollectionTest - a) containsAll failed", set
.IsSupersetOf(myCollection));
col.Remove(101);
assertTrue("CollectionTest - b) containsAll failed", !set
.IsSupersetOf(myCollection));

// removeAll
//assertTrue("CollectionTest - a) removeAll failed", set
// .ExceptWith(myCollection));
//assertTrue("CollectionTest - b) removeAll failed", !set
// .ExceptWith(myCollection)); // should not change the colletion
// // the 2nd time around

set.ExceptWith(myCollection); // Does not return in .NET
assertTrue("CollectionTest - c) removeAll failed", !set
.Contains(102));
assertTrue("CollectionTest - d) removeAll failed", !set
.Contains(103));

// retianAll
set.UnionWith(myCollection);
//assertTrue("CollectionTest - a) retainAll failed", set
// .IntersectWith(myCollection));
//assertTrue("CollectionTest - b) retainAll failed", !set
// .IntersectWith(myCollection)); // should not change the colletion
// // the 2nd time around

set.IntersectWith(myCollection); // Does not return in .NET
assertTrue("CollectionTest - c) retainAll failed", set
.IsSupersetOf(myCollection));
assertTrue("CollectionTest - d) retainAll failed", !set
.Contains(0));
assertTrue("CollectionTest - e) retainAll failed", !set
.Contains(50));

}

// clear
col.Clear();
assertTrue("CollectionTest - a) clear failed", col.Count == 0);
assertTrue("CollectionTest - b) clear failed", !col
.Contains(101));

}

}
}
48 changes: 48 additions & 0 deletions src/Lucene.Net.Tests/Support/Support_SetTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Adapted from Apache Harmony tests: https://github.com/apache/harmony/blob/trunk/classlib/support/src/test/java/tests/support/Support_SetTest.java
using Lucene.Net.Util;
using System.Collections.Generic;

namespace Lucene.Net
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

public class Support_SetTest : LuceneTestCase
{
ISet<int> set; // must contain the Integers 0 to 99

// LUCENENET: removed unused string argument and overload
public Support_SetTest(/*String p1,*/ ISet<int> s)
//: base(p1)
{
set = s;
}

public void RunTest() {
// add
assertTrue("Set Test - Adding a duplicate element changed the set",
!set.Add(50));
assertTrue("Set Test - Removing an element did not change the set", set
.Remove(50));
assertTrue(
"Set Test - Adding and removing a duplicate element failed to remove it",
!set.Contains(50));
set.Add(50);
new Support_CollectionTest(set).RunTest();
}
}
}
112 changes: 112 additions & 0 deletions src/Lucene.Net.Tests/Support/Support_UnmodifiableCollectionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Adapted from Apache Harmony tests via J2N: https://github.com/NightOwl888/J2N/blob/main/tests/NUnit/J2N.Tests/Collections/Support_UnmodifiableCollectionTest.cs

using Lucene.Net.Util;
using System.Collections.Generic;
using System.Linq;

namespace Lucene.Net
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

public class Support_UnmodifiableCollectionTest : LuceneTestCase
{
readonly ICollection<int> col;

// must be a collection containing the Integers 0 to 99 (which will iterate
// in order)

// LUCENENET: removed unused string argument and overload
public Support_UnmodifiableCollectionTest(/*String p1,*/ ICollection<int> c)
//: base(p1)
{
col = c;
}

public void RunTest()
{

// contains
assertTrue("UnmodifiableCollectionTest - should contain 0", col
.Contains(0));
assertTrue("UnmodifiableCollectionTest - should contain 50", col
.Contains(50));
assertTrue("UnmodifiableCollectionTest - should not contain 100", !col
.Contains(100));

// containsAll
HashSet<int> hs = new HashSet<int>();
hs.Add(0);
hs.Add(25);
hs.Add(99);
assertTrue(
"UnmodifiableCollectionTest - should contain set of 0, 25, and 99",
col.Intersect(hs).Count() == hs.Count); // Contains all
hs.Add(100);
assertTrue(
"UnmodifiableCollectionTest - should not contain set of 0, 25, 99 and 100",
col.Intersect(hs).Count() != hs.Count); // Doesn't contain all

// isEmpty
assertTrue("UnmodifiableCollectionTest - should not be empty", col.Count > 0);

// iterator
IEnumerator<int> it = col.GetEnumerator();
SortedSet<int> ss = new SortedSet<int>();
while (it.MoveNext())
{
ss.Add(it.Current);
}
it = ss.GetEnumerator();
for (int counter = 0; it.MoveNext(); counter++)
{
int nextValue = it.Current;
assertTrue(
"UnmodifiableCollectionTest - Iterator returned wrong value. Wanted: "
+ counter + " got: " + nextValue,
nextValue == counter);
}

// size
assertTrue(
"UnmodifiableCollectionTest - returned wrong size. Wanted 100, got: "
+ col.Count, col.Count == 100);

// toArray
object[] objArray;
objArray = col.Cast<object>().ToArray();
it = ss.GetEnumerator(); // J2N: Bug in Harmony, this needs to be reset to run
for (int counter = 0; it.MoveNext(); counter++)
{
assertTrue(
"UnmodifiableCollectionTest - toArray returned incorrect array",
(int)objArray[counter] == it.Current);
}

// toArray (Object[])
var intArray = new int[100];
col.CopyTo(intArray, 0);
it = ss.GetEnumerator(); // J2N: Bug in Harmony, this needs to be reset to run
for (int counter = 0; it.MoveNext(); counter++)
{
assertTrue(
"UnmodifiableCollectionTest - CopyTo(object[], int) filled array incorrectly",
intArray[counter] == it.Current);
}
}
}
}
Loading

0 comments on commit 6b161d9

Please sign in to comment.