summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsudden6 <sudden6@gmx.at>2016-12-11 17:45:03 +0100
committeriphydf <iphydf@users.noreply.github.com>2016-12-13 02:06:25 +0000
commit3cfe5544b1cb01771b5a1f722311502127265d0b (patch)
tree4c6887dd506dbfd20df8709a6a2e0046c123cc10
parent2dc2ac52b93e3a922337294c1e32f7bed794788f (diff)
Fix version compatibility test.
Also added some test cases for it.
-rw-r--r--CMakeLists.txt2
-rw-r--r--auto_tests/version_test.c93
-rw-r--r--toxcore/tox.api.h23
-rw-r--r--toxcore/tox.c6
-rw-r--r--toxcore/tox.h25
5 files changed, 131 insertions, 18 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 58979a34..2bad7282 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -423,6 +423,8 @@ auto_test(tox)
423auto_test(tox_many) 423auto_test(tox_many)
424auto_test(tox_many_tcp) 424auto_test(tox_many_tcp)
425auto_test(tox_one) 425auto_test(tox_one)
426auto_test(version)
427
426if(BUILD_TOXAV) 428if(BUILD_TOXAV)
427 auto_test(toxav_basic) 429 auto_test(toxav_basic)
428 auto_test(toxav_many) 430 auto_test(toxav_many)
diff --git a/auto_tests/version_test.c b/auto_tests/version_test.c
new file mode 100644
index 00000000..5ceff595
--- /dev/null
+++ b/auto_tests/version_test.c
@@ -0,0 +1,93 @@
1#include "../toxcore/tox.h"
2
3#include <stdio.h>
4#include <stdlib.h>
5
6#define check(major, minor, patch, expected) \
7 do_check(TOX_VERSION_MAJOR, TOX_VERSION_MINOR, TOX_VERSION_PATCH, \
8 major, minor, patch, \
9 TOX_VERSION_IS_API_COMPATIBLE(major, minor, patch), expected,\
10 &result)
11
12static void do_check(int lib_major, int lib_minor, int lib_patch,
13 int cli_major, int cli_minor, int cli_patch,
14 bool actual, bool expected,
15 int *result)
16{
17 if (actual != expected) {
18 printf("Client version %d.%d.%d is%s compatible with library version %d.%d.%d, but it should%s be\n",
19 cli_major, cli_minor, cli_patch, actual ? "" : " not",
20 lib_major, lib_minor, lib_patch, expected ? "" : " not");
21 *result = EXIT_FAILURE;
22 }
23}
24
25#undef TOX_VERSION_MAJOR
26#undef TOX_VERSION_MINOR
27#undef TOX_VERSION_PATCH
28
29int main(void)
30{
31 int result = 0;
32#define TOX_VERSION_MAJOR 0
33#define TOX_VERSION_MINOR 0
34#define TOX_VERSION_PATCH 4
35 check(0, 0, 0, false);
36 check(0, 0, 4, true);
37 check(0, 0, 5, false);
38 check(1, 0, 4, false);
39#undef TOX_VERSION_MAJOR
40#undef TOX_VERSION_MINOR
41#undef TOX_VERSION_PATCH
42
43#define TOX_VERSION_MAJOR 0
44#define TOX_VERSION_MINOR 1
45#define TOX_VERSION_PATCH 4
46 check(0, 0, 0, false);
47 check(0, 0, 4, false);
48 check(0, 0, 5, false);
49 check(0, 1, 0, true);
50 check(0, 1, 4, true);
51 check(0, 1, 5, false);
52 check(0, 2, 0, false);
53 check(0, 2, 4, false);
54 check(0, 2, 5, false);
55 check(1, 0, 0, false);
56 check(1, 0, 4, false);
57 check(1, 0, 5, false);
58#undef TOX_VERSION_MAJOR
59#undef TOX_VERSION_MINOR
60#undef TOX_VERSION_PATCH
61
62#define TOX_VERSION_MAJOR 1
63#define TOX_VERSION_MINOR 0
64#define TOX_VERSION_PATCH 4
65 check(1, 0, 0, true);
66 check(1, 0, 1, true);
67 check(1, 0, 4, true);
68 check(1, 0, 5, false);
69 check(1, 1, 0, false);
70#undef TOX_VERSION_MAJOR
71#undef TOX_VERSION_MINOR
72#undef TOX_VERSION_PATCH
73
74#define TOX_VERSION_MAJOR 1
75#define TOX_VERSION_MINOR 1
76#define TOX_VERSION_PATCH 4
77 check(1, 0, 0, true);
78 check(1, 0, 4, true);
79 check(1, 0, 5, true);
80 check(1, 1, 0, true);
81 check(1, 1, 1, true);
82 check(1, 1, 4, true);
83 check(1, 1, 5, false);
84 check(1, 2, 0, false);
85 check(1, 2, 4, false);
86 check(1, 2, 5, false);
87 check(2, 0, 0, false);
88#undef TOX_VERSION_MAJOR
89#undef TOX_VERSION_MINOR
90#undef TOX_VERSION_PATCH
91
92 return result;
93}
diff --git a/toxcore/tox.api.h b/toxcore/tox.api.h
index 723ce18b..2717bd02 100644
--- a/toxcore/tox.api.h
+++ b/toxcore/tox.api.h
@@ -183,13 +183,24 @@ const VERSION_PATCH = 5;
183 183
184/** 184/**
185 * A macro to check at preprocessing time whether the client code is compatible 185 * A macro to check at preprocessing time whether the client code is compatible
186 * with the installed version of Tox. 186 * with the installed version of Tox. Leading zeros in the version number are
187 * ignored. E.g. 0.1.5 is to 0.1.4 what 1.5 is to 1.4, that is: it can add new
188 * features, but can't break the API.
187 */ 189 */
188#define TOX_VERSION_IS_API_COMPATIBLE(MAJOR, MINOR, PATCH) \ 190#define TOX_VERSION_IS_API_COMPATIBLE(MAJOR, MINOR, PATCH) \
189 (TOX_VERSION_MAJOR == MAJOR && \ 191 (TOX_VERSION_MAJOR > 0 && TOX_VERSION_MAJOR == MAJOR) && ( \
190 (TOX_VERSION_MINOR > MINOR || \ 192 /* 1.x.x, 2.x.x, etc. with matching major version. */ \
191 (TOX_VERSION_MINOR == MINOR && \ 193 TOX_VERSION_MINOR > MINOR || \
192 TOX_VERSION_PATCH >= PATCH))) 194 TOX_VERSION_MINOR == MINOR && TOX_VERSION_PATCH >= PATCH \
195 ) || (TOX_VERSION_MAJOR == 0 && MAJOR == 0) && ( \
196 /* 0.x.x makes minor behave like major above. */ \
197 (TOX_VERSION_MINOR > 0 && TOX_VERSION_MINOR == MINOR) && ( \
198 TOX_VERSION_PATCH >= PATCH \
199 ) || (TOX_VERSION_MINOR == 0 && MINOR == 0) && ( \
200 /* 0.0.x and 0.0.y are only compatible if x == y. */ \
201 TOX_VERSION_PATCH == PATCH \
202 ) \
203 )
193 204
194/** 205/**
195 * A macro to make compilation fail if the client code is not compatible with 206 * A macro to make compilation fail if the client code is not compatible with
diff --git a/toxcore/tox.c b/toxcore/tox.c
index f964e18c..5d239be2 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -86,11 +86,7 @@ uint32_t tox_version_patch(void)
86 86
87bool tox_version_is_compatible(uint32_t major, uint32_t minor, uint32_t patch) 87bool tox_version_is_compatible(uint32_t major, uint32_t minor, uint32_t patch)
88{ 88{
89 return (TOX_VERSION_MAJOR == major && /* Force the major version */ 89 return TOX_VERSION_IS_API_COMPATIBLE(major, minor, patch);
90 (TOX_VERSION_MINOR > minor || /* Current minor version must be newer than requested -- or -- */
91 (TOX_VERSION_MINOR == minor && TOX_VERSION_PATCH >= patch) /* the patch must be the same or newer */
92 )
93 );
94} 90}
95 91
96 92
diff --git a/toxcore/tox.h b/toxcore/tox.h
index 4998609d..6d23aaf8 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -186,13 +186,24 @@ uint32_t tox_version_patch(void);
186 186
187/** 187/**
188 * A macro to check at preprocessing time whether the client code is compatible 188 * A macro to check at preprocessing time whether the client code is compatible
189 * with the installed version of Tox. 189 * with the installed version of Tox. Leading zeros in the version number are
190 */ 190 * ignored. E.g. 0.1.5 is to 0.1.4 what 1.5 is to 1.4, that is: it can add new
191#define TOX_VERSION_IS_API_COMPATIBLE(MAJOR, MINOR, PATCH) \ 191 * features, but can't break the API.
192 (TOX_VERSION_MAJOR == MAJOR && \ 192 */
193 (TOX_VERSION_MINOR > MINOR || \ 193#define TOX_VERSION_IS_API_COMPATIBLE(MAJOR, MINOR, PATCH) \
194 (TOX_VERSION_MINOR == MINOR && \ 194 (TOX_VERSION_MAJOR > 0 && TOX_VERSION_MAJOR == MAJOR) && ( \
195 TOX_VERSION_PATCH >= PATCH))) 195 /* 1.x.x, 2.x.x, etc. with matching major version. */ \
196 TOX_VERSION_MINOR > MINOR || \
197 TOX_VERSION_MINOR == MINOR && TOX_VERSION_PATCH >= PATCH \
198 ) || (TOX_VERSION_MAJOR == 0 && MAJOR == 0) && ( \
199 /* 0.x.x makes minor behave like major above. */ \
200 (TOX_VERSION_MINOR > 0 && TOX_VERSION_MINOR == MINOR) && ( \
201 TOX_VERSION_PATCH >= PATCH \
202 ) || (TOX_VERSION_MINOR == 0 && MINOR == 0) && ( \
203 /* 0.0.x and 0.0.y are only compatible if x == y. */ \
204 TOX_VERSION_PATCH == PATCH \
205 ) \
206 )
196 207
197/** 208/**
198 * A macro to make compilation fail if the client code is not compatible with 209 * A macro to make compilation fail if the client code is not compatible with