Browse Source

GCD of empty list of durations is zero, not None

Joeri Exelmans 5 years ago
parent
commit
71e8267e7d
2 changed files with 15 additions and 1 deletions
  1. 1 1
      src/sccd/util/duration.py
  2. 14 0
      src/sccd/util/test_duration.py

+ 1 - 1
src/sccd/util/duration.py

@@ -107,7 +107,7 @@ def gcd_pair(x: Duration, y: Duration) -> Duration:
   return Duration(gcd, x_converted.unit).normalize()
 
 def gcd(*iterable) -> Duration:
-  g = None
+  g = Duration(0)
   for d in iterable:
     if g is None:
       g = d

+ 14 - 0
src/sccd/util/test_duration.py

@@ -65,3 +65,17 @@ class TestDuration(unittest.TestCase):
     u = gcd(*l)
 
     self.assertEqual(u, Duration(1, Microsecond))
+
+  def test_gcd_few(self):
+    l = [Duration(3, Microsecond)]
+
+    u = gcd(*l)
+
+    self.assertEqual(u, Duration(3, Microsecond))
+
+  def test_gcd_none(self):
+    l = []
+
+    u = gcd(*l)
+
+    self.assertEqual(u, Duration(0))