[kernel] r17169 - in dists/trunk/linux-base: bin debian lib lib/t

Ben Hutchings benh at alioth.debian.org
Fri Apr 1 01:52:51 UTC 2011


Author: benh
Date: Fri Apr  1 01:52:49 2011
New Revision: 17169

Log:
Move version_cmp() into a module and its test cases into a separate script

Added:
   dists/trunk/linux-base/lib/
   dists/trunk/linux-base/lib/DebianLinux.pm
   dists/trunk/linux-base/lib/t/
   dists/trunk/linux-base/lib/t/DebianLinux.t
Modified:
   dists/trunk/linux-base/bin/linux-version
   dists/trunk/linux-base/debian/linux-base.install
   dists/trunk/linux-base/debian/rules

Modified: dists/trunk/linux-base/bin/linux-version
==============================================================================
--- dists/trunk/linux-base/bin/linux-version	Thu Mar 31 22:47:31 2011	(r17168)
+++ dists/trunk/linux-base/bin/linux-version	Fri Apr  1 01:52:49 2011	(r17169)
@@ -19,51 +19,7 @@
 use strict;
 use warnings;
 
-sub version_split {
-    # Split into numbers, hyphens with optional non-numeric suffixes
-    # (for pre-releases), and strings of any other characters
-    my $version = shift;
-    return $version =~ /(?:\d+|-\D*|[^-\d]+)/g;
-}
-
-sub version_cmp {
-    my ($left_ver, $right_ver) = @_;
-    my @left_comp = version_split($left_ver);
-    my @right_comp = version_split($right_ver);
-
-    for (my $i = 0; ; $i++) {
-	my $left = $left_comp[$i];
-	my $right = $right_comp[$i];
-	# Do the components indicate pre-releases?
-	my $left_pre = defined($left) && $left =~ /^-(?:rc|trunk)$/;
-	my $right_pre = defined($right) && $right =~ /^-(?:rc|trunk)$/;
-	# Are the components numeric?
-	my $left_num = defined($left) && $left =~ /^\d+/;
-	my $right_num = defined($right) && $right =~ /^\d+/;
-
-	# Pre-releases sort before anything, even end-of-string
-	if ($left_pre or $right_pre) {
-	    return -1 if !$right_pre;
-	    return 1 if !$left_pre;
-	}
-	# End-of-string sorts before anything else.
-	# End-of-string on both sides means equality.
-	if (!defined($left) or !defined($right)) {
-	    return -1 if defined($right);
-	    return defined($left) || 0;
-	}
-	# Use numeric comparison if both sides numeric.
-	# Otherwise use ASCII comparison.
-	if ($left_num && $right_num) {
-	    return -1 if $left < $right;
-	    return 1 if $left > $right;
-	} else {
-	    # Note that '.' > '-' thus 2.6.x.y > 2.6.x-z for any y, z.
-	    return -1 if $left lt $right;
-	    return 1 if $left gt $right;
-	}
-    }
-}
+use DebianLinux qw(version_cmp);
 
 sub usage {
     my $fh = shift;
@@ -113,48 +69,6 @@
     exit 0;
 }
 
-sub test_version_cmp {
-    use Test;
-    plan test => 27;
-    # Simple numeric comparison
-    ok(version_cmp('2', '2'), 0);
-    ok(version_cmp('2', '3'), -1);
-    ok(version_cmp('3', '2'), 1);
-    # Multiple components
-    ok(version_cmp('2.6.32', '2.6.32'), 0);
-    ok(version_cmp('2.6.32', '2.6.33'), -1);
-    ok(version_cmp('2.6.33', '2.6.32'), 1);
-    # Extra components (non-numeric, non-pre-release) > null
-    ok(version_cmp('2.6.32-local', '2.6.32-local'), 0);
-    ok(version_cmp('2.6.32', '2.6.32-local'), -1);
-    ok(version_cmp('2.6.32-local', '2.6.32'), 1);
-    # Extra numeric components > null
-    ok(version_cmp('2.6.32', '2.6.32.1'), -1);
-    ok(version_cmp('2.6.32.1', '2.6.32'), 1);
-    ok(version_cmp('2.6.32', '2.6.32-1'), -1);
-    ok(version_cmp('2.6.32-1', '2.6.32'), 1);
-    # Extra pre-release components < null
-    ok(version_cmp('2.6.33-rc1', '2.6.33-rc1'), 0);
-    ok(version_cmp('2.6.33-rc1', '2.6.33'), -1);
-    ok(version_cmp('2.6.33', '2.6.33-rc1'), 1);
-    ok(version_cmp('2.6.33-trunk', '2.6.33-trunk'), 0);
-    ok(version_cmp('2.6.33-rc1', '2.6.33-trunk'), -1);
-    ok(version_cmp('2.6.33-trunk', '2.6.33'), -1);
-    # Pre-release < numeric
-    ok(version_cmp('2.6.32-1', '2.6.32-trunk'), 1);
-    ok(version_cmp('2.6.32-trunk', '2.6.32-1'), -1);
-    # Pre-release < non-numeric non-pre-release
-    ok(version_cmp('2.6.32-local', '2.6.32-trunk'), 1);
-    ok(version_cmp('2.6.32-trunk', '2.6.32-local'), -1);
-    # Numeric < non-numeric non-pre-release
-    ok(version_cmp('2.6.32-1', '2.6.32-local'), -1);
-    ok(version_cmp('2.6.32-local', '2.6.32-1'), 1);
-    # Hyphen < dot
-    ok(version_cmp('2.6.32-2', '2.6.32.1'), -1);
-    ok(version_cmp('2.6.32.1', '2.6.32-2'), 1);
-    exit 0;
-}
-
 if (@ARGV == 0) {
     usage_error();
 }
@@ -167,7 +81,5 @@
     compare_versions(@ARGV);
 } elsif ($command eq 'sort') {
     sort_versions(@ARGV);
-} elsif ($command eq 'test') {
-    test_version_cmp();
 }
 usage_error();

Modified: dists/trunk/linux-base/debian/linux-base.install
==============================================================================
--- dists/trunk/linux-base/debian/linux-base.install	Thu Mar 31 22:47:31 2011	(r17168)
+++ dists/trunk/linux-base/debian/linux-base.install	Fri Apr  1 01:52:49 2011	(r17169)
@@ -1,2 +1,3 @@
 bin/linux-version usr/bin
 bin/perf usr/bin
+lib/DebianLinux.pm usr/share/perl5

Modified: dists/trunk/linux-base/debian/rules
==============================================================================
--- dists/trunk/linux-base/debian/rules	Thu Mar 31 22:47:31 2011	(r17168)
+++ dists/trunk/linux-base/debian/rules	Fri Apr  1 01:52:49 2011	(r17169)
@@ -18,6 +18,7 @@
 	dh_strip
 	dh_compress
 	dh_fixperms
+	dh_perl
 	dh_installdeb
 	dh_shlibdeps
 	dh_gencontrol

Added: dists/trunk/linux-base/lib/DebianLinux.pm
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ dists/trunk/linux-base/lib/DebianLinux.pm	Fri Apr  1 01:52:49 2011	(r17169)
@@ -0,0 +1,74 @@
+# Copyright 2011 Ben Hutchings
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+package DebianLinux;
+
+use strict;
+use warnings;
+
+BEGIN {
+    use Exporter ();
+    our @ISA = qw(Exporter);
+    our @EXPORT_OK = qw(version_cmp);
+}
+
+sub version_split {
+    # Split into numbers, hyphens with optional non-numeric suffixes
+    # (for pre-releases), and strings of any other characters
+    my $version = shift;
+    return $version =~ /(?:\d+|-\D*|[^-\d]+)/g;
+}
+
+sub version_cmp {
+    my ($left_ver, $right_ver) = @_;
+    my @left_comp = version_split($left_ver);
+    my @right_comp = version_split($right_ver);
+
+    for (my $i = 0; ; $i++) {
+	my $left = $left_comp[$i];
+	my $right = $right_comp[$i];
+	# Do the components indicate pre-releases?
+	my $left_pre = defined($left) && $left =~ /^-(?:rc|trunk)$/;
+	my $right_pre = defined($right) && $right =~ /^-(?:rc|trunk)$/;
+	# Are the components numeric?
+	my $left_num = defined($left) && $left =~ /^\d+/;
+	my $right_num = defined($right) && $right =~ /^\d+/;
+
+	# Pre-releases sort before anything, even end-of-string
+	if ($left_pre or $right_pre) {
+	    return -1 if !$right_pre;
+	    return 1 if !$left_pre;
+	}
+	# End-of-string sorts before anything else.
+	# End-of-string on both sides means equality.
+	if (!defined($left) or !defined($right)) {
+	    return -1 if defined($right);
+	    return defined($left) || 0;
+	}
+	# Use numeric comparison if both sides numeric.
+	# Otherwise use ASCII comparison.
+	if ($left_num && $right_num) {
+	    return -1 if $left < $right;
+	    return 1 if $left > $right;
+	} else {
+	    # Note that '.' > '-' thus 2.6.x.y > 2.6.x-z for any y, z.
+	    return -1 if $left lt $right;
+	    return 1 if $left gt $right;
+	}
+    }
+}
+
+1;

Added: dists/trunk/linux-base/lib/t/DebianLinux.t
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ dists/trunk/linux-base/lib/t/DebianLinux.t	Fri Apr  1 01:52:49 2011	(r17169)
@@ -0,0 +1,46 @@
+use strict;
+use warnings;
+use Test;
+
+use DebianLinux qw(version_cmp);
+
+BEGIN {
+    plan test => 27;
+}
+
+# Simple numeric comparison
+ok(version_cmp('2', '2'), 0);
+ok(version_cmp('2', '3'), -1);
+ok(version_cmp('3', '2'), 1);
+# Multiple components
+ok(version_cmp('2.6.32', '2.6.32'), 0);
+ok(version_cmp('2.6.32', '2.6.33'), -1);
+ok(version_cmp('2.6.33', '2.6.32'), 1);
+# Extra components (non-numeric, non-pre-release) > null
+ok(version_cmp('2.6.32-local', '2.6.32-local'), 0);
+ok(version_cmp('2.6.32', '2.6.32-local'), -1);
+ok(version_cmp('2.6.32-local', '2.6.32'), 1);
+# Extra numeric components > null
+ok(version_cmp('2.6.32', '2.6.32.1'), -1);
+ok(version_cmp('2.6.32.1', '2.6.32'), 1);
+ok(version_cmp('2.6.32', '2.6.32-1'), -1);
+ok(version_cmp('2.6.32-1', '2.6.32'), 1);
+# Extra pre-release components < null
+ok(version_cmp('2.6.33-rc1', '2.6.33-rc1'), 0);
+ok(version_cmp('2.6.33-rc1', '2.6.33'), -1);
+ok(version_cmp('2.6.33', '2.6.33-rc1'), 1);
+ok(version_cmp('2.6.33-trunk', '2.6.33-trunk'), 0);
+ok(version_cmp('2.6.33-rc1', '2.6.33-trunk'), -1);
+ok(version_cmp('2.6.33-trunk', '2.6.33'), -1);
+# Pre-release < numeric
+ok(version_cmp('2.6.32-1', '2.6.32-trunk'), 1);
+ok(version_cmp('2.6.32-trunk', '2.6.32-1'), -1);
+# Pre-release < non-numeric non-pre-release
+ok(version_cmp('2.6.32-local', '2.6.32-trunk'), 1);
+ok(version_cmp('2.6.32-trunk', '2.6.32-local'), -1);
+# Numeric < non-numeric non-pre-release
+ok(version_cmp('2.6.32-1', '2.6.32-local'), -1);
+ok(version_cmp('2.6.32-local', '2.6.32-1'), 1);
+# Hyphen < dot
+ok(version_cmp('2.6.32-2', '2.6.32.1'), -1);
+ok(version_cmp('2.6.32.1', '2.6.32-2'), 1);



More information about the Kernel-svn-changes mailing list