Skip to content

Commit

Permalink
Add Harmony Support_ unit tests, #1117
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Feb 13, 2025
1 parent 58524d3 commit 70869cd
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 7 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);
}
}
}
}
11 changes: 5 additions & 6 deletions src/Lucene.Net.Tests/Support/TestConcurrentHashSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public void TestSynchronizedSet()
HashSet<object> smallSet = new HashSet<object>();
for (int i = 0; i < 50; i++)
{
smallSet.add(objArray[i]);
smallSet.Add(objArray[i]);
}

const int numberOfLoops = 200;
Expand Down Expand Up @@ -454,15 +454,14 @@ public void TestSynchronizedSet()
smallSet = new HashSet<object>();
for (int i = 0; i < 100; i++)
{
smallSet.add(objArray[i]);
smallSet.Add(objArray[i]);
}
// LUCENENET TODO: could port this class and all of the classes it uses
// new Support_SetTest(new ConcurrentHashSet<object>(smallSet))
// .RunTest();
new Support_SetTest(new ConcurrentHashSet<int>(smallSet.Cast<int>())) // LUCENENET: add cast to int
.RunTest();

//Test self reference
mySet = new ConcurrentHashSet<object?>(smallSet); // was: Collections.synchronizedSet(smallSet);
mySet.add(mySet); // LUCENENET specific - references are not the same when wrapping via constructor, so adding mySet instead of smallSet
mySet.Add(mySet); // LUCENENET specific - references are not the same when wrapping via constructor, so adding mySet instead of smallSet
assertTrue("should contain self ref", Collections.ToString(mySet).IndexOf("(this", StringComparison.Ordinal) > -1);
}
}
Expand Down
33 changes: 32 additions & 1 deletion src/Lucene.Net/Support/ConcurrentHashSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,38 @@ public void ExceptWith(IEnumerable<T> other)

public void IntersectWith(IEnumerable<T> other)
{
throw new NotImplementedException();
if (other is null)
throw new ArgumentNullException(nameof(other));

var locksAcquired = 0;
try
{
AcquireAllLocks(ref locksAcquired);

if (CountInternal == 0)
{
return;
}

if (ReferenceEquals(this, other))
{
return;
}

var otherSet = new HashSet<T>(other, _comparer);

foreach (var item in this)
{
if (!otherSet.Contains(item))
{
TryRemoveInternal(item, GetItemHashCode(item), acquireLock: false);
}
}
}
finally
{
ReleaseLocks(0, locksAcquired);
}
}

public bool IsProperSubsetOf(IEnumerable<T> other)
Expand Down

0 comments on commit 70869cd

Please sign in to comment.