Пример #1
0
            public void WithNulls()
            {
                (var cube1, var cube2) = CubePoints();

                Assert.IsTrue(CubeUtils.AreObjectsCubes(new object [] { cube1, null, cube2 }, true, out var cubeType));
                Assert.AreEqual(typeof(DoubleValue), cubeType);
            }
Пример #2
0
            public void NoOverlappingAxes()
            {
                var axisSet1 = new AxisSet(new Axis("Axis 1", typeof(string)));
                var axisSet2 = new AxisSet(new Axis("Axis 2", typeof(string)));

                var cube1 = new Cube <DoubleValue>(axisSet1);
                var cube2 = new Cube <DoubleValue>(axisSet2);

                Assert.IsFalse(CubeUtils.TryCombineObjectsAsCubes(new object [] { cube1, cube2 }, out var cube, out var cubeValueType));
            }
Пример #3
0
            public void DifferentValueTypes()
            {
                var axes1 = new Axis [] { new Axis("Axis 1", typeof(string)) };
                var axes2 = new Axis [] { new Axis("Axis 1", typeof(string)) };

                var cube1 = new Cube <DoubleValue>(new AxisSet(axes1));
                var cube2 = new Cube <LongValue>(new AxisSet(axes2));

                Assert.IsFalse(CubeUtils.AreObjectsCubes(new object [] { cube1, cube2 }, true, out var cubeType));
            }
Пример #4
0
            public void Axes_Subset()
            {
                (var cube1, var cube2) = CubePoints();

                var combinedCube = CubeUtils.CombineCubes(new [] { cube1, cube2 }, new string [] { "str3" });

                Assert.AreEqual(1, combinedCube.AxisSet.Count);
                Assert.IsTrue(combinedCube.AxisSet.Any(axis => axis.Name == "str3"));

                Assert.AreEqual(1, combinedCube.Count());

                var val = combinedCube.Single();

                Assert.AreEqual(11, val.CubeValue.Value);
            }
Пример #5
0
        /// <summary>
        /// Attempt to combine the supplied set of objects as a cube if possible
        /// </summary>
        /// <param name="objects">The set of objects to combine (may contain nulls)</param>
        /// <param name="combinedCube">The combined cube if appropriate</param>
        /// <param name="combinedCubeCubeValueType">The combined cube's value type if appropriate</param>
        /// <returns>True if the objects could be combined, otherwise false</returns>
        public static bool TryCombineObjectsAsCubes(IEnumerable <object> objects, out object combinedCube, out Type combinedCubeCubeValueType)
        {
            if (objects == null)
            {
                throw new ArgumentNullException(nameof(objects));
            }

            var objectsAsList = objects.Where(o => o != null).ToList();

            if (!CubeUtils.AreObjectsCubes(objectsAsList, true, out combinedCubeCubeValueType))
            {
                combinedCube = null;
                combinedCubeCubeValueType = null;
                return(false);
            }

            var convertTypeMethod = typeof(System.Linq.Enumerable).GetMethod(nameof(System.Linq.Enumerable.Cast), new Type [] { typeof(System.Collections.IEnumerable) });

            var genericCubeType   = typeof(ICube <>).MakeGenericType(combinedCubeCubeValueType);
            var castGenericMethod = convertTypeMethod.MakeGenericMethod(genericCubeType);

            var objectsAsConvertedEnumerable = castGenericMethod.Invoke(null, new object [] { objectsAsList });

            // At this point, we know that they're all of the same type, but it's still possible that they
            // couldn't be combined due to various reasons, so be safe...
            var canCombineCubesMethod        = typeof(CubeUtils).GetMethod(nameof(CanCombineCubes));
            var canCombineCubesMethodGeneric = canCombineCubesMethod.MakeGenericMethod(combinedCubeCubeValueType);
            var canCombine = (bool)canCombineCubesMethodGeneric.Invoke(null, new object [] { objectsAsConvertedEnumerable });

            if (!canCombine)
            {
                combinedCube = null;
                combinedCubeCubeValueType = null;
                return(false);
            }

            // at this point, there's no reason why we can't combine them, so time to do so
            var combineCubesMethod        = typeof(CubeUtils).GetMethod(nameof(CombineCubes));
            var combineCubesMethodGeneric = combineCubesMethod.MakeGenericMethod(combinedCubeCubeValueType);

            combinedCube = combineCubesMethodGeneric.Invoke(null, new object [] { objectsAsConvertedEnumerable, null });
            return(true);
        }
Пример #6
0
            public void Axes_Order2()
            {
                (var cube1, var cube2) = CubePoints();

                var combinedCube = CubeUtils.CombineCubes(new [] { cube1, cube2 }, new string [] { "str3", "str1" });

                Assert.AreEqual(2, combinedCube.AxisSet.Count);
                Assert.IsTrue(combinedCube.AxisSet.Any(axis => axis.Name == "str1"));
                Assert.IsTrue(combinedCube.AxisSet.Any(axis => axis.Name == "str3"));

                var axisNames = combinedCube.AxisSet.Select(a => a.Name).ToList();

                Assert.AreEqual("str3", axisNames [0]);
                Assert.AreEqual("str1", axisNames [1]);

                Assert.AreEqual(1, combinedCube.Count());

                var val = combinedCube.Single();

                Assert.AreEqual(11, val.CubeValue.Value);
            }
Пример #7
0
 public void OnlyNulls()
 {
     Assert.IsFalse(CubeUtils.AreObjectsCubes(new object [] { null, null }, false, out var cubeType));
 }
Пример #8
0
 public void NullSet()
 {
     Assert.ThrowsException <ArgumentNullException>(() => { CubeUtils.AreObjectsCubes(null, false, out var cubeType); });
 }
Пример #9
0
 public void EmptySet()
 {
     Assert.IsFalse(CubeUtils.AreObjectsCubes(Array.Empty <object>(), false, out var cubeType));
 }
Пример #10
0
 public void EmptyInputs()
 {
     Assert.IsFalse(CubeUtils.TryCombineObjectsAsCubes(new object [0], out var combinedCube, out var type));
 }
Пример #11
0
 public void NullArgs()
 {
     Assert.ThrowsException <ArgumentNullException>(() => CubeUtils.TryCombineObjectsAsCubes(null, out var combinedCube, out var combinedCubeCubeValueType));
 }