Skip to content

Commit

Permalink
Merge branch 'master' into dev/dr/RevertFastAsyncLock
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb authored Jun 28, 2018
2 parents 2e87eec + 3c49fbe commit d88a9bc
Show file tree
Hide file tree
Showing 5 changed files with 857 additions and 662 deletions.
74 changes: 74 additions & 0 deletions src/Uno.Core.Tests/Collections/ListExtensionsFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// ******************************************************************
// Copyright � 2015-2018 nventive inc. All rights reserved.
//
// 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;
using System.Collections;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Uno.Extensions;

namespace Uno.Core.Tests.Collections
{
[TestClass]
public class ListExtensionsFixture
{
private static readonly int[] list = new [] { 1, 2, 3};

[TestMethod]
public void AsReadOnly()
{
IList<int> list2 = new List<int>(list);

Assert.IsTrue(list2.AsReadOnly().IsReadOnly);
}

[TestMethod]
public void Adapt()
{
IList<string> adapter = list.Adapt<int, string>();

Assert.IsNotNull(adapter);
}

[TestMethod]
public void IndexOf_WithComparer()
{
var @true = new MyComparer((l, r) => true);
var @false = new MyComparer((l, r) => false);

Assert.AreEqual(0, new[] {1, 2, 3}.IndexOf(2, @true));
Assert.AreEqual(1, new[] {1, 2, 3}.IndexOf(2, EqualityComparer<object>.Default));
Assert.AreEqual(1, new[] {1, 2, 3}.IndexOf(2, default(IEqualityComparer)));
Assert.AreEqual(-1, new[] {1, 2, 3}.IndexOf(2, @false));

Assert.AreEqual(0, new[] {1, 2, 3}.IndexOf(-1, @true));
Assert.AreEqual(-1, new[] {1, 2, 3}.IndexOf(-1, EqualityComparer<object>.Default));
Assert.AreEqual(-1, new[] {1, 2, 3}.IndexOf(-1, default(IEqualityComparer)));
Assert.AreEqual(-1, new[] {1, 2, 3}.IndexOf(-1, @false));
}

private class MyComparer : IEqualityComparer
{
private readonly Func<object, object, bool> _equals;

public MyComparer(Func<object, object, bool> equals) => _equals = @equals;

public bool Equals(object left, object right) => _equals(left, right);

public int GetHashCode(object obj) => throw new System.NotImplementedException();
}
}
}
53 changes: 53 additions & 0 deletions src/Uno.Core.Tests/FuncsFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// ******************************************************************
// Copyright � 2015-2018 nventive inc. All rights reserved.
//
// 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;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Uno.Extensions;

namespace Uno.Core.Tests
{
[TestClass]
public class FuncsFixture
{
#if DEBUG
private const TestTimeout _timeout = TestTimeout.Infinite;
#else
private const int _timeout = 5000;
#endif

[TestMethod]
[Timeout(_timeout)]
public async Task When_AsLockedMemoized_ThenSameInstance()
{
var mre = new ManualResetEvent(false);

Func<object> myFunc = () => { mre.WaitOne(); return new object(); };
var asLockedMemoized = myFunc.AsLockedMemoized();

var a = Task.Run(asLockedMemoized);
var b = Task.Run(asLockedMemoized);

mre.Set();

await Task.WhenAll(a, b);

Assert.IsTrue(object.ReferenceEquals(a.Result, b.Result));
}
}
}
25 changes: 25 additions & 0 deletions src/Uno.Core/Collections/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,30 @@ public static T FindNearestItem<T>(this IReadOnlyList<T> list, Func<T, bool> pre
.ToDivergentEnumerable(startingAt)
.FirstOrDefault(predicate);
}

/// <summary>
/// Determines the index of a specific item in the <see cref="IList"/>.
/// </summary>
/// <param name="list">The source list to look into.</param>
/// <param name="value">The object to locate in the <see cref="IList"/>.</param>
/// <param name="comparer">The comparer to use to locate the <paramref name="value" />.</param>
/// <returns>The index of value if found in the list; otherwise, -1.</returns>
public static int IndexOf(this IList list, object value, IEqualityComparer comparer)
{
if (comparer == null)
{
return list.IndexOf(value);
}

for (var i = 0; i < list.Count; i++)
{
if (comparer.Equals(value, list[i]))
{
return i;
}
}

return -1;
}
}
}
Loading

0 comments on commit d88a9bc

Please sign in to comment.