Tanoda
ProduceConsumeBufferTest.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Threading;
11using NUnit.Framework;
12
13namespace Leap.Unity.Tests {
14
16
18
19 [SetUp]
20 public void Setup() {
21 buffer = new ProduceConsumeBuffer<TestStruct>(16);
22 }
23
24 [TearDown]
25 public void Teardown() {
26 buffer = null;
27 }
28
29 [Test]
30 [Timeout(1000)]
31 public void Test() {
32 Thread consumer = new Thread(new ThreadStart(consumerThread));
33 Thread producer = new Thread(new ThreadStart(producerThread));
34
35 consumer.Start();
36 producer.Start();
37
38 consumer.Join();
39 producer.Join();
40 }
41
42 private void consumerThread() {
43 try {
44 for (int i = 0; i < buffer.Capacity; i++) {
45 TestStruct s;
46 s.index = i;
47 s.name = i.ToString();
48 while (!buffer.TryEnqueue(ref s)) { }
49 }
50 } catch (Exception e) {
51 Assert.Fail(e.Message);
52 }
53 }
54
55 private void producerThread() {
56 try {
57 for (int i = 0; i < buffer.Capacity; i++) {
58 TestStruct s;
59 while (!buffer.TryDequeue(out s)) { }
60
61 Assert.That(s.index, Is.EqualTo(i));
62 Assert.That(s.name, Is.EqualTo(i.ToString()));
63 }
64 } catch (Exception e) {
65 Assert.Fail(e.Message);
66 }
67 }
68
69 private struct TestStruct {
70 public int index;
71 public string name;
72 }
73 }
74}
int Capacity
Returns the maximum number of elements that the buffer can hold.
bool TryEnqueue(ref T t)
Tries to enqueue a value into the buffer. If the buffer is already full, this method will perform no ...
bool TryDequeue(out T t)
Tries to dequeue a value off of the buffer. If the buffer is empty this method will perform no action...