From: Matteo Nastasi (mop) Date: Thu, 14 Nov 2013 07:29:15 +0000 (+0100) Subject: versions_cmp function added with its own test (and test infrastructure) X-Git-Tag: v4.11.0~33 X-Git-Url: http://mop.ddnsfree.com/gitweb/?a=commitdiff_plain;h=4cde4a448fe46347671b8d21a088626772af4987;p=brisk.git versions_cmp function added with its own test (and test infrastructure) --- 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 @@ + \ 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 @@ + \ 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 + '', '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 Matteo Nasta
version '.$G_brisk_version.'

Copyright 2006-2012
Matteo Nastasi (aka mop)

'); +// 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);