Created
November 6, 2013 12:17
-
-
Save mathiassoeholm/7335171 to your computer and use it in GitHub Desktop.
More complicated Linq test, Average failed on iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEngine; | |
public class LinqTest : MonoBehaviour | |
{ | |
public class MyClass | |
{ | |
public Vector3 MyVector; | |
} | |
public class Foo<T> where T : new() | |
{ | |
public T Bar; | |
// Random collection used to test SelectMany | |
public List<T> Collection = new List<T> | |
{ | |
new T(), | |
new T(), | |
new T(), | |
new T(), | |
}; | |
} | |
private Dictionary<string, Foo<MyClass>> _testTarget; | |
void Start() | |
{ | |
_testTarget = new Dictionary<string, Foo<MyClass>>() | |
{ | |
{ "AAA", new Foo<MyClass> { Bar = new MyClass{ MyVector = new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100)) } } }, | |
{ "BBB", new Foo<MyClass> { Bar = new MyClass{ MyVector = new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100)) } } }, | |
{ "CCC", new Foo<MyClass> { Bar = new MyClass{ MyVector = new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100)) } } }, | |
{ "DDD", new Foo<MyClass> { Bar = new MyClass{ MyVector = new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100)) } } }, | |
{ "EEE", new Foo<MyClass> { Bar = new MyClass{ MyVector = new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100)) } } }, | |
{ "FFF", new Foo<MyClass> { Bar = new MyClass{ MyVector = new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100)) } } }, | |
{ "GGG", new Foo<MyClass> { Bar = new MyClass{ MyVector = new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100)) } } }, | |
}; | |
} | |
void OnGUI() | |
{ | |
if (GUILayout.Button("Sum projection")) | |
{ | |
Debug.Log(_testTarget.Sum(x => x.Value.Bar.MyVector.x)); | |
} | |
if (GUILayout.Button("Select")) | |
{ | |
Debug.Log(_testTarget.Select(x => x.Value.Bar.MyVector.z).ElementAt(0)); | |
} | |
if (GUILayout.Button("Select anonymous type")) | |
{ | |
Debug.Log(_testTarget.Select(x => new {Name = "Bob", Age = x.Value.Bar.MyVector.y }).ElementAt(0).Name); | |
} | |
if (GUILayout.Button("Where")) | |
{ | |
Debug.Log(_testTarget.Where(x => x.Value.Bar.MyVector.x < 0).ElementAt(0).Value.Bar.MyVector.y); | |
} | |
if (GUILayout.Button("Select many")) | |
{ | |
Debug.Log(_testTarget.SelectMany(x => x.Value.Collection).ElementAt(0).MyVector.x); | |
} | |
if (GUILayout.Button("Any")) | |
{ | |
Debug.Log(_testTarget.Any(x => x.Value.Bar.MyVector.y > 50)); | |
} | |
if (GUILayout.Button("Min")) | |
{ | |
Debug.Log(_testTarget.Min(x => x.Value.Bar.MyVector.z)); | |
} | |
if (GUILayout.Button("Max")) | |
{ | |
Debug.Log(_testTarget.Max(x => x.Value.Bar.MyVector.z)); | |
} | |
if (GUILayout.Button("Average")) // Bad | |
{ | |
Debug.Log(_testTarget.Average(x => x.Value.Bar.MyVector.z)); | |
} | |
if (GUILayout.Button("Count")) | |
{ | |
Debug.Log(_testTarget.Count(x => x.Value.Bar.MyVector.x < 0)); | |
} | |
if (GUILayout.Button("Aggregate")) | |
{ | |
Debug.Log(_testTarget.Aggregate((x, y) => new KeyValuePair<string, Foo<MyClass>>(x.Key, y.Value)).Value.Bar.MyVector.y); | |
} | |
if (GUILayout.Button("All")) | |
{ | |
Debug.Log(_testTarget.All(x => x.Value.Bar.MyVector.y < 90)); | |
} | |
if (GUILayout.Button("First or default")) | |
{ | |
Debug.Log(_testTarget.FirstOrDefault(x => x.Value.Bar.MyVector.y > 40).Value.Bar.MyVector.y); | |
} | |
if (GUILayout.Button("First")) | |
{ | |
Debug.Log(_testTarget.First(x => x.Key == "CCC").Value.Bar.MyVector.y); | |
} | |
if (GUILayout.Button("Take")) | |
{ | |
Debug.Log(_testTarget.Take(10).ElementAt(0).Value.Bar.MyVector.y); | |
} | |
if (GUILayout.Button("Take while")) | |
{ | |
Debug.Log(_testTarget.TakeWhile(x => x.Value.Bar.MyVector.x < 30).ElementAt(0).Value.Bar.MyVector.y); | |
} | |
if (GUILayout.Button("Skip")) | |
{ | |
Debug.Log(_testTarget.Skip(4).ElementAt(0).Value.Bar.MyVector.y); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment