예제 #1
0
def sortedArrayRandomTest(size):
    """
    Creates a SortedArray object and an normal Python list with random elements of a given size.
    Compares the contents of the two objects after insertions and deletions.
    Returns False if any test shows not equal; otherwise returns True.
    Uses for testing the SortedArray class.
    """
    s_arr = SortedArray()
    ar = []

    # insertions
    for i in range(size):
        n = random.randint(-10000, 10000)
        ar.append(n)
        s_arr.insert(n)
    ar.sort()
    if not sortedArrayCompare(s_arr, ar): return False

    # known deletions
    for i in range(size // 2):
        index = random.randint(0, len(ar) - 1)
        n = ar[i]
        s_arr.delete(n)
        ar.remove(n)
    if not sortedArrayCompare(s_arr, ar): return False

    # random deletions
    for i in range(size // 4):
        n = random.randint(-10000, 10000)
        if n in ar:
            ar.remove(n)
        s_arr.delete(n)
    if not sortedArrayCompare(s_arr, ar): return False

    # more insertions
    for i in range(size // 4):
        n = random.randint(-10000, 10000)
        ar.append(n)
        s_arr.insert(n)
    ar.sort()
    if not sortedArrayCompare(s_arr, ar): return False

    # overwrites
    for i in range(size // 4):
        index = random.randint(0, len(ar) - 1)
        value = random.randint(-10000, 10000)
        ar[index] = value
        ar.sort()
        s_arr[index] = value
    if not sortedArrayCompare(s_arr, ar): return False

    return True
예제 #2
0
 def test_get_out_of_bounds(self):
     array = SortedArray(5)
     array.put(1)
     array.put(5)
     array.put(3)
     with self.assertRaises(IndexError):
         array.get(4)
예제 #3
0
 def test_put_to_full(self):
     array = SortedArray(3)
     array.put(1)
     array.put(5)
     array.put(3)
     with self.assertRaises(ValueError):
         array.put(4)
예제 #4
0
def sortedArrayFullTest():
    """
    Various tests for SortedArray class, including insertion, deletion, and overwrites.
    """
    init = [1, 5, 2, 80, 42, 232, 10, -1]
    adds = [2, 20, 423, 65, 4, 98, -1, 5, 10]
    dels = [42, 232, -1, 5, 2000, -10]
    test = [1, 2, 80, 10, 2, 20, 423, 65, 4, 98, 5, 10, 555]
    test.sort()

    print "\nTesting SortedArray: various operations"
    print "Target sums: init=", sum(
        init), "after adds=", sum(init) + sum(adds), "test=", sum(test)

    s_arr = SortedArray(init)
    print "After initial: s_arr =", s_arr, "sum = ", sum(s_arr)
    for a in adds:
        s_arr.insert(a)
    print "After adds: s_arr =", s_arr, "sum = ", sum(s_arr)

    for d in dels:
        s_arr.delete(d)
    print "After deletes: s_arr =", s_arr, "sum = ", sum(s_arr)

    s_arr[0] = 555

    print "After setting element i: s_arr =", s_arr, "sum = ", sum(s_arr)
    print s_arr.minimum(), s_arr.maximum()

    error = False
    for i in range(len(test)):
        if test[i] != s_arr[i]:
            print "Error at index", i, " test =", test[i], " s_arr =", s_arr[i]

    print "\nTesting SortedArray: random operations"
    for i in range(20):
        if sortedArrayRandomTest(500): print "Test", i + 1, "successful"
        else: print "Failure at test", i + 1
예제 #5
0
 def test_find_non_existing(self):
     array = SortedArray(5)
     array.put(1)
     array.put(5)
     array.put(3)
     self.assertEqual(array.find(4), -1)
예제 #6
0
 def test_put_to_top(self):
     array = SortedArray(5)
     array.put(1)
     array.put(2)
     array.put(3)
     self.assertEqual(array.array, [1, 2, 3, None, None])
예제 #7
0
 def test_get_existing(self):
     array = SortedArray(3)
     array.put(1)
     array.put(5)
     array.put(3)
     self.assertEqual(array.get(1), 3)
예제 #8
0
 def test_put_to_bottom(self):
     array = SortedArray(5)
     array.put(3)
     array.put(5)
     array.put(1)
     self.assertEqual(array.array, [1, 3, 5, None, None])
예제 #9
0
def find_nearest_neighbors(q, k, train_data):
    neighbors = SortedArray(k)
    for x in train_data:
        dist = calc_dist(q, x)
        neighbors.add((x, dist))
    return neighbors.array