Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix class "IntegerList.Bits". #3391

Merged
merged 3 commits into from
Mar 10, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix class "IntegerList.Bits".
The class that I added last week was not storing the correct values at all.
Added unit test "IntegerListTest"
  • Loading branch information
nickshulman committed Mar 10, 2025
commit f4576697c41c233355d83c99159e812e6ab85831
13 changes: 9 additions & 4 deletions pwiz_tools/Shared/CommonUtil/Collections/IntegerList.cs
Original file line number Diff line number Diff line change
@@ -76,16 +76,20 @@ public static ImmutableList<int> FromIntegers(IEnumerable<int> values)
private class Bits : IntegerList
{
private readonly int _count;
private readonly BitVector32[] _bits;
private readonly int[] _bits;

public Bits(int count, IEnumerable<bool> boolValues)
{
_count = count;
_bits = new BitVector32[(count + 31) / 32];
_bits = new int[(count + 31) / 32];
int index = 0;
foreach (var boolValue in boolValues)
{
_bits[index / 32][index % 32] = boolValue;
if (boolValue)
{
_bits[index / 32] |= 1 << (index % 32);
}
index++;
}
}

@@ -108,7 +112,8 @@ public override int this[int index]
throw new IndexOutOfRangeException();
}

return _bits[index / 32][index % 32] ? 1 : 0;
var mask = 1 << (index % 32);
return 0 == (_bits[index / 32] & mask) ? 0 : 1;
}
}

63 changes: 63 additions & 0 deletions pwiz_tools/Skyline/Test/IntegerListTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Original author: Nicholas Shulman <nicksh .at. u.washington.edu>,
* MacCoss Lab, Department of Genome Sciences, UW
*
* Copyright 2025 University of Washington - Seattle, WA
*
* Licensed 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.
*/

using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using pwiz.Common.Collections;
using pwiz.SkylineTestUtil;

namespace pwiz.SkylineTest
{
[TestClass]
public class IntegerListTest : AbstractUnitTest
{
[TestMethod]
public void TestIntegerListFromIntegers()
{
var bits = IntegerListFromIntegers(Enumerable.Repeat(0, 100).Concat(Enumerable.Repeat(1, 100)));
Assert.AreEqual("Bits", bits.GetType().Name);
var constantList = IntegerListFromIntegers(Enumerable.Repeat(1000, 100));
Assert.IsInstanceOfType(constantList, typeof(ConstantList<int>));
var bytes = IntegerListFromIntegers(Enumerable.Range(0, 255));
Assert.AreEqual("Bytes", bytes.GetType().Name);
var shorts = IntegerListFromIntegers(Enumerable.Range(0, 512));
Assert.AreEqual("Shorts", shorts.GetType().Name);
IntegerListFromIntegers(Enumerable.Range(99999, 100));
}

private ImmutableList<int> IntegerListFromIntegers(IEnumerable<int> values)
{
var list = values.ToList();
var intList = IntegerList.FromIntegers(list);
CollectionAssert.AreEqual(list, intList.ToList());
if (list.Count == 0)
{
Assert.AreSame(ImmutableList.Empty<int>(), intList);
}

if (list.Count > 1 && list.Distinct().Count() == 1)
{
Assert.IsInstanceOfType(intList, typeof(ConstantList<int>));
}

return intList;
}
}
}
1 change: 1 addition & 0 deletions pwiz_tools/Skyline/Test/Test.csproj
Original file line number Diff line number Diff line change
@@ -175,6 +175,7 @@
<Compile Include="ElibQuantifiableTransitionTest.cs" />
<Compile Include="ErrorIfPersistentFilesDirModifiedTest.cs" />
<Compile Include="FastaSequenceTest.cs" />
<Compile Include="IntegerListTest.cs" />
<Compile Include="LocalizedHtmlTutorialsTest.cs" />
<Compile Include="LibraryRankedSpectrumInfoTest.cs" />
<Compile Include="MassListIonsTest.cs" />