Просмотр исходного кода

add some tests for binary conversion

Bert Temme 2 лет назад
Родитель
Сommit
09f3ffd6d2

+ 13 - 0
src/SharpGLTF.Cesium/BinaryTable.cs

@@ -14,6 +14,19 @@ namespace SharpGLTF
     /// </summary>
     public static class BinaryTable
     {
+        public static List<byte> GetBytesForArray<T>(List<List<T>> values)
+        {
+            var bytes = new List<byte>();
+            foreach (var value in values)
+            {
+                var b = GetBytes(value);
+                bytes.AddRange(b);
+            }
+
+            return bytes;
+        }
+
+
         /// <summary>
         /// Converts a list of primitive types into a byte array
         /// </summary>

+ 1 - 6
src/SharpGLTF.Cesium/Schema2/EXTStructuralMetaDataRoot.cs

@@ -147,12 +147,7 @@ namespace SharpGLTF.Schema2
 
         private static int GetBufferView<T>(this ModelRoot model, List<List<T>> values)
         {
-            var bytes = new List<byte>();
-            foreach (var value in values)
-            {
-                var b = BinaryTable.GetBytes(value);
-                bytes.AddRange(b);
-            }
+            List<byte> bytes = BinaryTable.GetBytesForArray(values);
             var bufferView = model.UseBufferView(bytes.ToArray());
             int logicalIndex = bufferView.LogicalIndex;
             return logicalIndex;

+ 17 - 6
tests/SharpGLTF.Cesium.Tests/BinaryTableTests.cs

@@ -7,6 +7,16 @@ namespace SharpGLTF
 {
     public class BinaryTableTests
     {
+        [Test]
+        public void ConvertListofListofIntToBytes()
+        {
+            var values = new List<List<int>>();
+            values.Add(new List<int>() { 0, 1 });
+            values.Add(new List<int>() { 2, 3 });
+            var bytes = BinaryTable.GetBytesForArray(values);
+            Assert.That(bytes.Count, Is.EqualTo(BinaryTable.GetSize<int>() * 4));
+        }
+
         [Test]
         public void ConvertVector2ToBytes()
         {
@@ -90,19 +100,20 @@ namespace SharpGLTF
             bytes = BinaryTable.GetBytes(GetTestArray<int>());
             Assert.That(bytes.Length, Is.EqualTo(BinaryTable.GetSize<int>() * 2));
             
-            //bytes = BinaryTable.GetBytes(new List<string>() { "a", "b" });
-            //Assert.That(bytes.Length, Is.EqualTo(2));
+            bytes = BinaryTable.GetBytes(new List<string>() { "a", "b" });
+            Assert.That(bytes.Length, Is.EqualTo(2));
 
             bytes  = BinaryTable.GetBytes(new List<bool>() { true, false });
             Assert.That(bytes.Length, Is.EqualTo(1));
-            // create a bit arrat frin the byte array
+            
             var bits = new System.Collections.BitArray(bytes);
             Assert.That(bits[0] == true);
             Assert.That(bits[1] == false);
 
-            var ints = new List<List<int>>();
-            ints.Add(new List<int>() { 0, 1 });
-            Assert.Throws<NotImplementedException>(() => BinaryTable.GetBytes(ints));
+            // test exception for datetime
+            var dates = new List<DateTime>();
+            dates.Add(new DateTime());
+            Assert.Throws<NotImplementedException>(() => BinaryTable.GetBytes(dates));
         }
 
         private static List<T> GetTestArray<T>()

+ 1 - 1
tests/SharpGLTF.Cesium.Tests/ExtStructuralMetadataTests.cs

@@ -169,7 +169,7 @@ namespace SharpGLTF.Cesium
             exampleEnum.Values.Add(new EnumValue() { Name = "ExampleEnumValueB", Value = 1 });
             exampleEnum.Values.Add(new EnumValue() { Name = "ExampleEnumValueC", Value = 2 });
 
-            schema.Enums.Add("exampleEnumType1", exampleEnum);
+            schema.Enums.Add("exampleEnumType", exampleEnum);
 
             // property tables