def testConstructYFromX(self): """Tests cA.constructXFromY using randomly generated examples. """ for test in range(self.testNo): #Do self.testNo random tests X = getRandomX() #If testConstructReversePrefixSortMatrix is failing then the #inputs to cA.constructCommonSuffixMatrix will be incorrect #and this test will likely fail. Hence checking that #cA.constructCommonSuffixMatrix works correctly first is likely #to be a good plan. A = cA.constructReversePrefixSortMatrix(X) Y = cA.constructYFromX(X) for j in range(A.shape[1]-1): #Check that Y[A[i,j],j] == X[i][j] self.assertEqual(list(map(lambda i : int(X[A[i,j]][j]), range(A.shape[0]))), list(Y[:,j]))
def testConstructCommonSuffixMatrix(self): """Tests cA.constructCommonSuffixMatrix using randomly generated examples, again checking the output using a simple brute force algorithm. """ for test in range(self.testNo): #Do self.testNo random tests X = getRandomX() #If testConstructReversePrefixSortMatrix is failing then the #inputs to cA.constructCommonSuffixMatrix will be incorrect #and this test will likely fail. A = cA.constructReversePrefixSortMatrix(X) D = cA.constructCommonSuffixMatrix(A, X) #Might un-comment the following for debugging #print "Test", test, "X", X, "D", D #Check has expected number of rows and columns self.assertEquals(A.shape, D.shape) #For each column check the length of the common suffixes for j in xrange(A.shape[1]): #A sorted list of reversed prefixes k = sorted(map(lambda i: X[i][:j][::-1], range(len(X)))) def commonPrefix(x1, x2): #Computes the common prefix of the two strings x1 and x2 i = 0 while i < len(x1) and x1[i] == x2[i]: i += 1 return x1[:i] #Convert k into the lengths of the common suffixes of #the strings of interest k = ([0] if len(X) > 0 else []) + \ map(lambda i : len(commonPrefix(k[i], k[i-1])), range(1, len(X))) #Check the common prefix lengths agree self.assertEquals(k, list(D[:, j]))
def testConstructReversePrefixSortMatrix(self): """Tests cA.constructReversePrefixSortMatrix using randomly generated examples, checking the output using a simple brute force algorithm. """ for test in range(self.testNo): #Do self.testNo random tests X = getRandomX() #A random set of equal length substrings A = cA.constructReversePrefixSortMatrix(X) #Might un-comment the following for debugging #print "Test", test, "X", X, "A", A #Check has expected number of rows self.assertEqual(len(X), A.shape[0]) #Check has expected number of columns self.assertEqual(len(X[0])+1 if len(X) > 0 else 1, A.shape[1]) #For each column check the reverse prefix sort for j in range(A.shape[1]): #Gets a sorted list of reversed prefixes #paired with the index of the parent string in X k = sorted(list(map(lambda i : (X[i][:j][::-1], i), range(len(X))))) #The lists of sorted indices should be equal self.assertEqual(list(map(lambda i : i[1], k)), list(A[:,j]))