drgn/tests/test_util.py
Omar Sandoval 7c7286ef8f util: add version comparison implementation
Add an verrevcmp() function based on the coreutils implementation (which
comes from gnulib, which is derived from the implementation in dpkg).
This will be used by vmtest.
2020-03-31 12:36:03 -07:00

26 lines
970 B
Python

from functools import cmp_to_key
import unittest
from util import Version, verrevcmp
class TestUtil(unittest.TestCase):
def assertVersionSort(self, sorted_list):
self.assertEqual(sorted(sorted_list, key=cmp_to_key(verrevcmp)), sorted_list)
def test_verrevcmp(self):
self.assertVersionSort(
["0~", "0", "1", "1.0", "1.1~rc1", "1.1~rc2", "1.1", "1.2", "1.12"]
)
self.assertVersionSort(["a", "."])
self.assertVersionSort(["", "1"])
self.assertVersionSort(["~", "~1"])
self.assertVersionSort(["~~", "~~a", "~", "", "a"])
def test_version(self):
self.assertLess(Version("1.0"), Version("2.0"))
self.assertLess(Version("5.6.0-rc6"), Version("5.6.0-rc7"))
self.assertLess(Version("5.6.0-rc7"), Version("5.6.0"))
self.assertLess(Version("5.6.0-rc7-vmtest2"), Version("5.6.0-vmtest1"))
self.assertLess(Version("5.6.0-vmtest1"), Version("5.6.0-vmtest2"))