]> git.meshlink.io Git - meshlink/blob - test/basicpp.cpp
Don't use assert() to check the results of pthread_*() calls.
[meshlink] / test / basicpp.cpp
1 #include <cstring>
2 #include <iostream>
3 #include <unistd.h>
4 #include <cerrno>
5 #include <cassert>
6 #include <dirent.h>
7
8 #include "meshlink++.h"
9
10 using namespace std;
11
12 int main(void) {
13         assert(meshlink::destroy("basicpp_conf"));
14
15         // Open a new meshlink instance.
16
17         {
18                 meshlink::mesh mesh("basicpp_conf", "foo", "basicpp", DEV_CLASS_BACKBONE);
19                 assert(mesh.isOpen());
20
21                 // Check that our own node exists.
22
23                 meshlink::node *self = mesh.get_self();
24                 assert(self);
25                 assert(!strcmp(self->name, "foo"));
26
27                 // Disable local discovery.
28
29                 mesh.enable_discovery(false);
30
31                 // Start and stop the mesh.
32
33                 assert(mesh.start());
34                 mesh.stop();
35
36                 // Make sure we can start and stop the mesh again.
37
38                 assert(mesh.start());
39                 mesh.stop();
40
41                 // Close the mesh and open it again, now with a different name parameter.
42
43                 mesh.close();
44                 assert(!mesh.open("basicpp_conf", "bar", "basicpp", DEV_CLASS_BACKBONE));
45
46                 // Open it without giving a name.
47
48                 assert(mesh.open("basicpp_conf", nullptr, "basicpp", DEV_CLASS_BACKBONE));
49
50                 // Check that the name is ignored now, and that we still are "foo".
51
52                 self = mesh.get_self();
53                 assert(self);
54                 assert(!strcmp(self->name, "foo"));
55
56                 // Start and stop the mesh.
57
58                 mesh.enable_discovery(false);
59
60                 assert(mesh.start());
61                 mesh.stop();
62         }
63
64         // Destroy the mesh.
65
66         assert(meshlink::destroy("basicpp_conf"));
67
68         DIR *dir = opendir("basicpp_conf");
69         assert(dir);
70         struct dirent *ent;
71         while((ent = readdir(dir))) {
72                 assert(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."));
73         }
74         closedir(dir);
75
76         return 0;
77 }