public void ImageMemoryTest() { SetImageStubImplementation(); CallCount count = NativeK4a.CountCalls(); Assert.AreEqual(0, count.Calls("k4a_image_create")); Assert.AreEqual(0, count.Calls("k4a_image_release")); Task.Run(() => { using (Image image = new Image(ImageFormat.Custom, 640, 480, 640 * 2)) { Memory <byte> memory = image.Memory; System.Span <byte> memorySpan = memory.Span; System.Span <short> shortSpan = MemoryMarshal.Cast <byte, short>(memorySpan); Assert.AreEqual(0, shortSpan[0]); Assert.AreEqual(1, shortSpan[1]); image.Dispose(); } }).Wait(); GC.Collect(0, GCCollectionMode.Forced, true, true); GC.WaitForPendingFinalizers(); Assert.AreEqual(count.Calls("k4a_image_reference") + 1, count.Calls("k4a_image_release"), "References not zero"); }
public void Temperature() { this.SetCaptureReleaseImplementation(); this.nativeK4a.SetImplementation(@" float k4a_capture_get_temperature_c(k4a_capture_t capture_handle) { STUB_ASSERT(capture_handle == (k4a_capture_t)0x0C001234); return 2.5f; } void k4a_capture_set_temperature_c(k4a_capture_t capture_handle, float value) { STUB_ASSERT(capture_handle == (k4a_capture_t)0x0C001234); STUB_ASSERT(value == 3.5f); } "); CallCount count = this.nativeK4a.CountCalls(); using (Capture c = new Capture()) { // Temperature values should not be cached, so every access should call the // native layer Assert.AreEqual(0, count.Calls("k4a_capture_get_temperature_c")); Assert.AreEqual(2.5f, c.Temperature); Assert.AreEqual(1, count.Calls("k4a_capture_get_temperature_c")); Assert.AreEqual(2.5f, c.Temperature); Assert.AreEqual(2, count.Calls("k4a_capture_get_temperature_c")); // Verify writes Assert.AreEqual(0, count.Calls("k4a_capture_set_temperature_c")); c.Temperature = 3.5f; Assert.AreEqual(1, count.Calls("k4a_capture_set_temperature_c")); // Verify the write is being marshaled correctly _ = Assert.Throws(typeof(NativeFailureException), () => { c.Temperature = 4.0f; }); c.Dispose(); // Verify disposed behavior _ = Assert.Throws(typeof(ObjectDisposedException), () => { c.Temperature = 4.0f; }); _ = Assert.Throws(typeof(ObjectDisposedException), () => { float x = c.Temperature; }); } }
public void CaptureGarbageCollection() { this.nativeK4a.SetImplementation(@" k4a_result_t k4a_capture_create(k4a_capture_t* capture_handle) { STUB_ASSERT(capture_handle != NULL); *capture_handle = (k4a_capture_t)0x0C001234; return K4A_RESULT_SUCCEEDED; } void k4a_capture_release(k4a_capture_t capture_handle) { STUB_ASSERT(capture_handle == (k4a_capture_t)0x0C001234); } k4a_result_t k4a_set_debug_message_handler( k4a_logging_message_cb_t *message_cb, void *message_cb_context, k4a_log_level_t min_level) { STUB_ASSERT(message_cb != NULL); return K4A_RESULT_SUCCEEDED; } "); CallCount count = this.nativeK4a.CountCalls(); Assert.AreEqual(0, count.Calls("k4a_capture_create")); Assert.AreEqual(0, count.Calls("k4a_capture_release")); WeakReference capture = this.CreateWithWeakReference(() => { Capture c = new Capture(); // The reference should still exist and we should have not seen close called Assert.AreEqual(1, count.Calls("k4a_capture_create")); Assert.AreEqual(0, count.Calls("k4a_capture_release")); return(c); }); // The reference to the Capture object is no longer on the stack, and therefore is free to be garbage collected // At this point capture.IsAlive is likely to be true, but not guaranteed to be // Force garbage collection GC.Collect(0, GCCollectionMode.Forced, true); GC.WaitForPendingFinalizers(); Assert.AreEqual(false, capture.IsAlive); // k4a_capture_release should have been called automatically Assert.AreEqual(1, count.Calls("k4a_capture_create")); Assert.AreEqual(1, count.Calls("k4a_capture_release")); }
public void ImageGarbageCollection() { SetImageStubImplementation(); CallCount count = NativeK4a.CountCalls(); Assert.AreEqual(0, count.Calls("k4a_image_create")); Assert.AreEqual(0, count.Calls("k4a_image_release")); System.WeakReference image = CreateWithWeakReference(() => { Image i = new Image(ImageFormat.Custom, 640, 480, 640 * 2); var memory = i.Memory; // The reference should still exist and we should have not seen close called Assert.AreEqual(1, count.Calls("k4a_image_create")); Assert.AreEqual(0, count.Calls("k4a_image_release")); return(i); }); // The reference to the Device object is no longer on the stack, and therefore is free to be garbage collected // At this point capture.IsAlive is likely to be true, but not garanteed to be // Force garbage collection System.GC.Collect(0, System.GCCollectionMode.Forced, true); System.GC.WaitForPendingFinalizers(); Assert.AreEqual(false, image.IsAlive); // k4a_device_close should have been called automatically Assert.AreEqual(1, count.Calls("k4a_image_create")); Assert.AreEqual(count.Calls("k4a_image_reference") + 1, count.Calls("k4a_image_release")); }