From 4cde4a448fe46347671b8d21a088626772af4987 Mon Sep 17 00:00:00 2001
From: "Matteo Nastasi (mop)" <nastasi@alternativeoutput.it>
Date: Thu, 14 Nov 2013 08:29:15 +0100
Subject: [PATCH] versions_cmp function added with its own test (and test
 infrastructure)

---
 test/Etc/brisk.conf.pho  |  3 +++
 test/Obj/test.phh        |  5 +++++
 test/versionlib_test.php | 41 ++++++++++++++++++++++++++++++++++++++++
 web/Obj/brisk.phh        | 30 +++++++++++++++++++++++++++++
 4 files changed, 79 insertions(+)
 create mode 100644 test/Etc/brisk.conf.pho
 create mode 100644 test/Obj/test.phh
 create mode 100755 test/versionlib_test.php

diff --git a/test/Etc/brisk.conf.pho b/test/Etc/brisk.conf.pho
new file mode 100644
index 0000000..a3abdc0
--- /dev/null
+++ b/test/Etc/brisk.conf.pho
@@ -0,0 +1,3 @@
+<?php
+$G_lang = 'it';
+?>
\ No newline at end of file
diff --git a/test/Obj/test.phh b/test/Obj/test.phh
new file mode 100644
index 0000000..6a65ba9
--- /dev/null
+++ b/test/Obj/test.phh
@@ -0,0 +1,5 @@
+<?php
+
+$DOCUMENT_ROOT = 'test';
+
+?>
\ No newline at end of file
diff --git a/test/versionlib_test.php b/test/versionlib_test.php
new file mode 100755
index 0000000..eacdc41
--- /dev/null
+++ b/test/versionlib_test.php
@@ -0,0 +1,41 @@
+#!/usr/bin/php
+<?php
+require_once('test/Obj/test.phh');
+require_once('web/Obj/brisk.phh');
+
+$arr = array(array('v1' => '', 'v2' => '', 'exp' => 0),
+
+             array('v1' => '1.2.3', 'v2' => '1.2.3', 'exp' => 0),
+
+             array('v1' => '1.2.3', 'v2' => '1.2', 'exp' => 0),
+             array('v1' => '1.2.3', 'v2' => '1', 'exp' => 0),
+
+             array('v1' => '1.2', 'v2' => '1.2.3', 'exp' => 0),
+             array('v1' => '1', 'v2' => '1.2.3', 'exp' => 0),
+
+             array('v1' => '1', 'v2' => '2', 'exp' => -1),
+             array('v1' => '2', 'v2' => '1', 'exp' => 1),
+
+             array('v1' => '0.1', 'v2' => '0.2', 'exp' => -1),
+             array('v1' => '0.2', 'v2' => '0.1', 'exp' => 1),
+
+             array('v1' => '0.0.1', 'v2' => '0.0.2', 'exp' => -1),
+             array('v1' => '0.0.2', 'v2' => '0.0.1', 'exp' => 1),
+
+             array('v1' => '0.0.2', 'v2' => '0.0.1', 'exp' => 1),
+             );
+
+$tb = '	';
+foreach($arr as $el) {
+    $ret = versions_cmp($el['v1'], $el['v2']);
+    printf("V1: [%s]\nV2: [%s]\nRet: [%+d]\n", $el['v1'], $el['v2'], $ret);
+    if ($ret != $el['exp']) {
+        printf("\nExp: [%+d] Ret and Exp differ!\n\n", $el['exp']);
+        exit(1);
+    }
+    else {
+        printf("\n");
+    }
+}
+exit(0);
+?>
diff --git a/web/Obj/brisk.phh b/web/Obj/brisk.phh
index a855f26..a2e1cb8 100644
--- a/web/Obj/brisk.phh
+++ b/web/Obj/brisk.phh
@@ -290,6 +290,36 @@ Copyright 2006-2012 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nasta
 <br><b>version '.$G_brisk_version.'</b><br><br>
 Copyright 2006-2012 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>');
 
+//  return values
+// -1 v1 < v2
+//  0 equal
+//  1 v1 > v2
+function versions_cmp($v1, $v2)
+{
+    // printf("V1: [%s]\nV2: [%s]\n", $v1, $v2);
+    if ($v1 == $v2)
+        return 0;
+
+    $v1_ar = split('\.', $v1);
+    $v2_ar = split('\.', $v2);
+
+    $v2_ct = count($v2_ar);
+
+    for ($i = 0 ; $i < count($v1_ar) ; $i++) {
+        if (($v2_ct - 1) < $i) {
+            break;
+        }
+        // printf("here [%s] [%s]\n", $v1_ar[$i], $v2_ar[$i]);
+        if ($v1_ar[$i] != $v2_ar[$i]) {
+            if (strval($v1_ar[$i]) < strval($v2_ar[$i]))
+                return -1;
+            else
+                return  1;
+        }
+    }
+    return 0;
+}
+
 function addrtoipv4($addr)
 {
     $ipv4addr_arr = explode(':' , $addr);
-- 
2.17.1