diff options
author | Rodrigo Arias Mallo <rodarima@gmail.com> | 2024-12-10 22:30:12 +0100 |
---|---|---|
committer | Rodrigo Arias Mallo <rodarima@gmail.com> | 2024-12-10 22:30:12 +0100 |
commit | 429d5f88b94ff28416cbfc6420b6389fa284df97 (patch) | |
tree | fb6fdaf7731de1ef396f98b748c56f3149801c84 /tests/test_version_cmp.c |
Import RTFL 0.1.1v0.1.1
Diffstat (limited to 'tests/test_version_cmp.c')
-rw-r--r-- | tests/test_version_cmp.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/test_version_cmp.c b/tests/test_version_cmp.c new file mode 100644 index 0000000..98667d6 --- /dev/null +++ b/tests/test_version_cmp.c @@ -0,0 +1,49 @@ +// Used for "../configure.ac". + +#include <ctype.h> +#include <stdlib.h> + +static int version_cmp (const char *v1, const char *v2) +{ + const char *s1 = v1, *s2 = v2; + while (*s1 && *s2) { + if (isdigit (*s1) && isdigit (*s2)) { + char buf1[10], buf2[10]; + int n1 = 0, n2 = 0; + + while (isdigit (*s1)) { + if (n1 < 9) buf1[n1++] = *s1; + s1++; + } + + while (isdigit (*s2)) { + if (n2 < 9) buf2[n2++] = *s2; + s2++; + } + + buf1[n1] = buf2[n2] = 0; + int c = atoi (buf1) - atoi (buf2); + if (c != 0) + return c; + } else { + if (*s1 != *s2) + return *s1 - *s2; + s1++; + s2++; + } + } + + return *s1 - *s2; +} + +#include <stdio.h> + +int main (int argc, char *argv[]) +{ + printf ("%d, %d, %d %d\n", + version_cmp ("0.1", "0.001"), + version_cmp ("0.1", "0.002"), + version_cmp ("0.1a", "0.1"), + version_cmp ("2.38.0", "2.38.1")); + return 0; +} |