Beispiel #1
0
    private IEnumerator Start()
    {
        dataStruct = new TestDataStruct()
        {
            list    = new NativeList <int>(100, Allocator.Persistent),
            hashMap = new NativeHashMap <int2, int>(100, Allocator.Persistent)
        };

        for (int i = 0; i < 100; i++)
        {
            dataStruct.list.Add(i);
            dataStruct.hashMap.TryAdd(new int2(i, i), i);
        }

        var job = new TestJob()
        {
            dataStruct = dataStruct
        };

        var jobHandler = job.Schedule(100, 10);

        yield return(new WaitUntil(() => jobHandler.IsCompleted));

        for (int i = 0; i < 100; i++)
        {
            print(dataStruct.list[i]);
        }
    }
Beispiel #2
0
        public void TestSerializeToJson()
        {
            var data = new TestDataStruct
            {
                SomeValue    = "hello there",
                SomeIntValue = 24789
            };

            string json = JsonHelper.SerializeToJson(data);

            Assert.AreEqual(
                "{\"some_int_value\":24789,\"some_missing_value\":null,\"some_value\":\"hello there\"}",
                json);
        }
            public void Should_Work_For_Some()
            {
                // Arrange
                var maybeStruct = Maybe.SomeStruct <int>(5);
                var testData    = new TestDataStruct()
                {
                    MaybeStruct = maybeStruct
                };

                // Act & Assert
                using (var stream = TestUtils.SerializeToStream(testData))
                {
                    var testDataDeserialized = (TestDataStruct)TestUtils.DeserializeFromStream(stream);

                    testDataDeserialized.MaybeStruct.ShouldBeOfType <MaybeStruct <int> >();
                    testDataDeserialized.MaybeStruct.HasValue.ShouldBeTrue();
                }
            }
    public IntPtr MarshalManagedToNative(object ManagedObj)
    {
        m_MarshaledInstance = (TestDataStruct)ManagedObj;
        IntPtr nativeData = Marshal.AllocHGlobal(GetNativeDataSize());

        if (m_MarshaledInstance != null)
        {
            unsafe     //unsafe is simpler but can easily be done without unsafe if necessary
            {
                byte *pData = (byte *)nativeData;
                *     pData = m_MarshaledInstance.data1;
                *(int *)(pData + 1) = m_MarshaledInstance.data2;
                Marshal.Copy(m_MarshaledInstance.data3, 0, (IntPtr)(pData + 5), 7);
                *(long *)(pData + 12) = m_MarshaledInstance.data4;
                *(pData + 20)         = m_MarshaledInstance.data5;
            }
        }
        return(nativeData);
    }
    public object MarshalNativeToManaged(IntPtr pNativeData)
    {
        TestDataStruct data = m_MarshaledInstance;

        m_MarshaledInstance = null;     //clear out TLS for next call.
        if (data == null)
        {
            data = new TestDataStruct(); //if no in object then return a new one
        }
        unsafe                           //unsafe is simpler but can easily be done without unsafe if necessary
        {
            byte *pData = (byte *)pNativeData;
            data.data1 = *pData;
            data.data2 = *(int *)(pData + 1);
            Marshal.Copy((IntPtr)(pData + 5), data.data3, 0, 7);
            data.data4 = *(long *)(pData + 12);
            data.data5 = *(pData + 20);
        }
        return(data);
    }
        public void StoreOne()
        {
            var collection = new JsonDataCollection <int, TestDataStruct>(TempFilePath);
            var obj        = new TestDataStruct {
                Id = 1, B = true, C = "Test"
            };

            collection[obj.Id] = obj;
            collection.Commit();

            collection.Clear();
            Assert.Empty(collection.All);
            collection.Refresh();

            Assert.Single(collection.All);
            var obj2 = collection[obj.Id];

            Assert.Equal(obj.B, obj2.B);
            Assert.Equal(obj.C, obj2.C);
        }
        public void RoundTrip3DArrayStruct()
        {
            var expected = new TestDataStruct[3, 4, 2];

            for (int z = 0; z < expected.GetLength(2); z++)
            {
                for (int y = 0; y < expected.GetLength(1); y++)
                {
                    for (int x = 0; x < expected.GetLength(0); x++)
                    {
                        expected[x, y, z] = new TestDataStruct {
                            A = x + y * 10 + z * 100, B = string.Format("X: {0} Y: {1} Z: {2}", x, y, z)
                        }
                    }
                }
            }
            ;

            CompileAndLoadAssets((TestDataStruct[, , ])expected.Clone(), result =>
            {
                Assert.IsInstanceOf <TestDataStruct[, , ]>(result);
                Assert.AreEqual(result.Rank, 3);

                for (int i = 0; i < result.Rank; i++)
                {
                    Assert.AreEqual(expected.GetLength(i), result.GetLength(i));
                }

                for (int z = 0; z < expected.GetLength(2); z++)
                {
                    for (int y = 0; y < expected.GetLength(1); y++)
                    {
                        for (int x = 0; x < expected.GetLength(0); x++)
                        {
                            Assert.AreEqual(expected[x, y, z], result[x, y, z]);
                        }
                    }
                }
            });
        }
 public static extern void TestDataMethod([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(TestDataMarshaler))] TestDataStruct pData);