Remove Cxxtest dependency
[zynaddsubfx-code.git] / src / Tests / MqTest.cpp
blob5f3f3650b292d09072d70e0dcc6149ac52a3f40a
1 /*
2 ZynAddSubFX - a software synthesizer
4 PluginTest.h - CxxTest for embedding zyn
5 Copyright (C) 2013-2013 Mark McCurry
6 Authors: Mark McCurry
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 #include "test-suite.h"
14 #include <cmath>
15 #include <cstdlib>
16 #include <iostream>
17 #include <fstream>
18 #include <string>
19 #include <thread>
20 #include <rtosc/thread-link.h>
21 #include <unistd.h>
22 #include "../Containers/MultiPseudoStack.h"
23 using namespace std;
24 using namespace zyn;
26 char *instance_name=(char*)"";
28 class MessageTest
30 public:
31 MultiQueue *s;
32 void setUp() {
33 s = new MultiQueue;
36 void tearDown() {
37 delete s;
40 void testBasic(void)
42 auto *mem = s->alloc();
43 TS_NON_NULL(mem);
44 TS_NON_NULL(mem->memory);
45 TS_ASSERT(!s->read());
46 s->write(mem);
47 auto *mem2 = s->read();
48 assert_ptr_eq(mem, mem2,
49 "both locations are the same", __LINE__);
50 s->free(mem2);
52 void testMed(void)
54 for(int i=0; i<100; ++i) {
55 auto *mem = s->alloc();
56 TS_NON_NULL(mem);
57 TS_NON_NULL(mem->memory);
58 TS_ASSERT(!s->read());
59 s->write(mem);
60 auto *mem2 = s->read();
61 assert_ptr_eq(mem, mem2,
62 "both locations are the same", __LINE__);
63 s->free(mem2);
67 #define OPS 1000
68 #define THREADS 8
69 void testThreads(void)
71 uint8_t messages[OPS*THREADS];
72 memset(messages, 0, sizeof(messages));
73 std::thread *t[THREADS];
74 for(int i=0; i<THREADS; ++i) {
75 t[i] = new std::thread([this,i,&messages](){
76 int op=0;
77 while(op<OPS) {
78 int read = rand()%2;
79 if(read) {
80 auto *mem = s->read();
81 if(mem) {
82 //printf("r%d",i%10);
83 //printf("got: <%s>\n", mem->memory);
84 messages[atoi(mem->memory)]++;
86 s->free(mem);
87 } else {
88 auto *mem = s->alloc();
89 if(mem) {
90 sprintf(mem->memory,"%d written by %d@op%d", i*OPS+op,i,op);
91 //printf("w%d",i%10);
92 op++;
94 s->write(mem);
97 });
100 printf("thread started...\n");
101 for(int i=0; i<THREADS; ++i) {
102 t[i]->join();
103 delete t[i];
105 printf("thread stopped...\n");
106 //read the last few
107 while(1) {
108 auto *mem = s->read();
109 if(mem) {
110 printf("got: <%s>\n", mem->memory);
111 messages[atoi(mem->memory)]++;
112 } else
113 break;
114 s->free(mem);
117 int good = 1;
118 for(int i=0; i<OPS*THREADS; ++i) {
119 if(messages[i] != 1) {
120 assert(false);
121 good = 0;
124 TS_ASSERT(good);
128 int main()
130 MessageTest test;
131 RUN_TEST(testBasic);
132 RUN_TEST(testMed);
133 RUN_TEST(testThreads);
134 return test_summary();