banshee r3923 - in trunk/banshee: . src/Libraries/Hyena/Hyena.Collections tests/Hyena



Author: abock
Date: Sat May 17 20:05:42 2008
New Revision: 3923
URL: http://svn.gnome.org/viewvc/banshee?rev=3923&view=rev

Log:
2008-05-17  Aaron Bockover  <abock gnome org>

    * src/Libraries/Hyena/Hyena.Collections/RangeCollection.cs: Made the
    indexer code more sexy so it doesn't look like I wrote it at 4:30 in
    the morning, ensure that negative indexes throw IOORE

    * tests/Hyena/RangeCollectionTests.cs: Added an indexer stress test and
    some extra specific tests for ensuring IndexOutOfRange is thrown when
    it should be



Modified:
   trunk/banshee/ChangeLog
   trunk/banshee/src/Libraries/Hyena/Hyena.Collections/RangeCollection.cs
   trunk/banshee/tests/Hyena/RangeCollectionTests.cs

Modified: trunk/banshee/src/Libraries/Hyena/Hyena.Collections/RangeCollection.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena.Collections/RangeCollection.cs	(original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena.Collections/RangeCollection.cs	Sat May 17 20:05:42 2008
@@ -33,7 +33,11 @@
 using System.Collections.Generic;
 #endif
 
+#if NET_1_1
+namespace System.Collections
+#else
 namespace Hyena.Collections
+#endif
 {
 #if NET_1_1
     internal
@@ -248,7 +252,7 @@
             return min;
         }
         
-        private int FindRangeIndexForValue (int value)
+        public int FindRangeIndexForValue (int value)
         {
             return Array.BinarySearch (ranges, 0, range_count, new Range (value, -1));
         }
@@ -308,14 +312,10 @@
 
         public int this[int index] {
             get {
-                int cuml_count = 0;
-                for (int r_i = 0; r_i < range_count; r_i++) {
-                    cuml_count += ranges[r_i].Count;
-                    if (index >= cuml_count) {
-                        continue;
+                for (int i = 0, cuml_count = 0; i < range_count && index >= 0; i++) {
+                    if (index < (cuml_count += ranges[i].Count)) {
+                        return ranges[i].End - (cuml_count - index) + 1;
                     }
-                    
-                    return ranges[r_i].End - (cuml_count - index) + 1;
                 }
                 
                 throw new IndexOutOfRangeException (index.ToString ());
@@ -326,13 +326,27 @@
 
 #region ICollection Implementation
 
-        public void Add (int value)
+        public bool Add (int value)
         {
             if (!Contains (value)) {
                 generation++;
                 InsertRange (new Range (value, value));
                 index_count++;
+                return true;
             }
+            
+            return false;
+        }
+        
+        void
+#if NET_2_0
+        ICollection<int>.
+#else
+        ICollection.
+#endif
+        Add (int value)
+        {
+            Add (value);
         }
                 
         public bool Remove (int value)
@@ -420,4 +434,3 @@
 
     }
 }
-

Modified: trunk/banshee/tests/Hyena/RangeCollectionTests.cs
==============================================================================
--- trunk/banshee/tests/Hyena/RangeCollectionTests.cs	(original)
+++ trunk/banshee/tests/Hyena/RangeCollectionTests.cs	Sat May 17 20:05:42 2008
@@ -4,7 +4,7 @@
 // Author:
 //   Aaron Bockover <abockover novell com>
 //
-// Copyright (C) 2007 Novell, Inc.
+// Copyright (C) 2007-2008 Novell, Inc.
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -27,6 +27,7 @@
 //
 
 using System;
+using System.Collections.Generic;
 using NUnit.Framework;
 using Hyena.Collections;
 
@@ -326,7 +327,7 @@
     }
     
     [Test]
-    public void TestIndexer ()
+    public void TestIndexerForGoodIndexes ()
     {
         RangeCollection range = new RangeCollection ();
         
@@ -365,4 +366,63 @@
         Assert.AreEqual (12, range[7]);
         Assert.AreEqual (13, range[8]);
     }
+    
+    [Test]
+    public void TestStressForGoodIndexes ()
+    {
+        Random random = new Random (0xbeef);
+        RangeCollection ranges = new RangeCollection ();
+        List<int> indexes = new List<int> ();
+        
+        for (int i = 0, n = 250000; i < n; i++) {
+            int value = random.Next (n);
+            if (ranges.Add (value)) {
+                indexes.Add (value);
+            }
+        }
+        
+        indexes.Sort ();
+        
+        Assert.AreEqual (indexes.Count, ranges.Count);
+        for (int i = 0; i < indexes.Count; i++) {
+            Assert.AreEqual (indexes[i], ranges[i]);
+        }
+    }
+    
+    [Test]
+    [ExpectedException (typeof (IndexOutOfRangeException))]
+    public void TestIndexerForNegativeBadIndex ()
+    {
+        RangeCollection range = new RangeCollection ();
+        Assert.AreEqual (0, range[1]);
+    }
+    
+    [Test]
+    [ExpectedException (typeof (IndexOutOfRangeException))]
+    public void TestIndexerForZeroBadIndex ()
+    {
+        RangeCollection range = new RangeCollection ();
+        Assert.AreEqual (0, range[0]);
+    }
+    
+    [Test]
+    [ExpectedException (typeof (IndexOutOfRangeException))]
+    public void TestIndexerForPositiveBadIndex ()
+    {
+        RangeCollection range = new RangeCollection ();
+        range.Add (1);
+        Assert.AreEqual (0, range[1]);
+    }
+    
+    [Test]
+    public void TestExplicitInterface ()
+    {
+        ICollection<int> range = new RangeCollection ();
+        range.Add (1);
+        range.Add (2);
+        range.Add (5);
+        range.Add (6);
+        
+        Assert.AreEqual (4, range.Count);
+    }
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]