Exemplo n.º 1
0
def getAveragePercentCoverage(chromosome):
    firstCoveredSum = 0
    getMaxLength(chromosome)
    #print 'getAPFD : ' + str(MaxLength)
    init_cov = '0' * len(CoverageList[0])
    for i in range(MaxLength):
        temp_cov = '0' * len(CoverageList[0])
        for g in range(len(chromosome)):
            if len(chromosome[g]) <= i:
                continue
            else:
                try:
                    temp_cov = bitarray.to01(
                        bitarray(temp_cov)
                        | bitarray(CoverageList[chromosome[g][i]]))
                except:
                    print str(len(chromosome)) + ' : ' + str(g)
                    print str(len(chromosome[g])) + ' : ' + str(i)
                    raw_input('getAPFD error ...')
        #diff_cov = bitdiff(bitarray(temp_cov),(bitarray(init_cov)&bitarray(temp_cov)))
        diff_list = getCoverageIndex(
            temp_cov, bitarray.to01(bitarray(init_cov) & bitarray(temp_cov)))
        init_cov = bitarray.to01(bitarray(temp_cov) | bitarray(init_cov))
        #firstCoveredSum += diff_cov * (i + 1)
        for df in diff_list:
            firstCoveredSum += ((i + 1) * CoverageNumber[df])
    try:
        AveragePercentageCoverage = 1 - firstCoveredSum / (TestNumber * sum(
            CoverageNumber) * 1.0) + 1 / (2 * TestNumber * 1.0)
    except:
        print 'test number : ' + str(TestNumber)
        print 'coverage number : ' + str(sum(CoverageNum))
    return AveragePercentageCoverage
Exemplo n.º 2
0
def greedyadditional(sametestcase_list, coverage_array, numberlist):
    coverage_row_array = []
    detected_mu = ""
    temp_list = copy.deepcopy(sametestcase_list)
    for i in range(len(coverage_array[0])):
        detected_mu += "0"
    detected_mu_init = detected_mu
    addtional_order = []
    for i in range(len(sametestcase_list)):
        count = 0
        max_number = int(0)
        max_order = int(-1)
        for j in range(len(sametestcase_list)):
            if sametestcase_list[j] not in addtional_order:
                #temp_index = testsuitlist_old.index(sametestcase_list[j])
                #temp_number = bitdiff(bitarray(detected_mu)&bitarray(coverage_array[j]),bitarray(coverage_array[j]))
                temp_difflist = getCoverageIndex(
                    bitarray.to01(
                        bitarray(detected_mu) & bitarray(coverage_array[j])),
                    coverage_array[j])
                temp_number = getAllCount(temp_difflist, numberlist)
                if temp_number > max_number and sametestcase_list[
                        j] not in addtional_order:
                    max_order = j
                    max_number = temp_number
            else:
                continue
        if max_order == int(-1):
            detected_mu = detected_mu_init
            for j in range(len(sametestcase_list)):
                if sametestcase_list[j] not in addtional_order:
                    #temp_index = testsuitlist_old.index(sametestcase_list[j])
                    #temp_number = bitdiff(bitarray(detected_mu)&bitarray(coverage_array[j]),bitarray(coverage_array[j]))
                    temp_difflist = getCoverageIndex(
                        bitarray.to01(
                            bitarray(detected_mu)
                            & bitarray(coverage_array[j])), coverage_array[j])
                    temp_number = getAllCount(temp_difflist, numberlist)
                    if temp_number > max_number and sametestcase_list[
                            j] not in addtional_order:
                        max_order = j
                        max_number = temp_number
                else:
                    continue
        if max_order != int(-1):
            addtional_order.append(sametestcase_list[max_order])
            temp_list.pop(temp_list.index(sametestcase_list[max_order]))
            #detected_mu = str(bitarray(detected_mu)|bitarray(coverage_array[max_order])).split("'")[1]
            detected_mu = bitarray.to01(
                bitarray(detected_mu) | bitarray(coverage_array[max_order]))
            continue
        elif max_order == int(-1):
            for item in temp_list:
                addtional_order.append(item)
            break
    return addtional_order
Exemplo n.º 3
0
def getJaccardDistance_old(a, b, index_list):
    if len(a) != len(b):
        print 'Error: length not equal.'
        #raw_input('pause...')
        assert (0)
    if a.count('1') == 0 and b.count('1') == 0:
        return 0
    length = len(a)
    distance = float(0)
    join = bitarray.to01(bitarray(a) & bitarray(b))
    combine = bitarray.to01(bitarray(a) | bitarray(b))
    join_count = 0
    combine_count = 0
    for i in range(len(join)):
        if join[i] == '1':
            join_count += index_list[i]
        if combine[i] == '1':
            combine_count += index_list[i]
    distance = 1 - (join_count / (combine_count * 1.0))
    return distance
Exemplo n.º 4
0
def getFirstTest(chromosome):
    max_time = MaxTime
    result_list = []
    result_dict = {}
    try:
        covered_cov = range(0, len(CoverageList[chromosome[0][0]]))
    except:
        print len(CoverageList)
        print chromosome
        print checknumber(chromosome)
        raw_input('getFirstTest error...')
    st = time.time()
    for group_item in chromosome:
        group_time = 0
        for test_item in group_item:
            group_time += TimeList[test_item]
            temp_count = max_time - group_time + 0.5 * TimeList[test_item]
            if group_time in result_dict.keys():
                result_dict[group_time].append(
                    (group_time, test_item, temp_count))
            else:
                result_dict[group_time] = [(group_time, test_item, temp_count)]
    tt = copy.deepcopy(result_dict.keys())
    tt.sort()
    init_cov = '0' * len(CoverageList[chromosome[0][0]])
    for item in tt:
        temp_cov = '0' * len(CoverageList[chromosome[0][0]])
        for test_item in result_dict[item]:
            temp_name = test_item[1]
            temp_cov = bitarray.to01(
                bitarray(temp_cov) | bitarray(CoverageList[temp_name]))
            diff_list = getCoverageIndex(
                temp_cov,
                bitarray.to01(bitarray(init_cov) & bitarray(temp_cov)))
            init_cov = bitarray.to01(bitarray(init_cov) | bitarray(temp_cov))
            for i in diff_list:
                result_list.append((test_item, CoverageNumber[i]))
    return result_list
Exemplo n.º 5
0
Arquivo: art.py Projeto: PTCP/PTCP
def getJaccardDistance(a, b, index_list):
    if len(a) != len(b):
        print 'Error: length not equal.'
        raw_input('pause...')
    if a.count('1') == 0 and b.count('1') == 0:
        return 0
    length = len(a)
    distance = float(0)
    join = bitarray.to01(bitarray(a) & bitarray(b))
    combine = bitarray.to01(bitarray(a) | bitarray(b))
    join_count = 0
    combine_count = 0
    #jdst = time.time()
    for i in range(len(join)):
        if join[i] == '1':
            join_count += index_list[i]
        if combine[i] == '1':
            combine_count += index_list[i]
    #print '1 : ' + str(time.time() - jdst)
    '''
    jdst = time.time()
    temp1 = [0]*length
    temp2 = [0]*length
    for i in range(len(join)):
        temp1[i] = int(join[i])
        temp2[i] = int(combine[i])
    
    t1 = np.dot(np.array(temp1),np.array(index_list))
    t2 = np.dot(np.array(temp2),np.array(index_list))
    print '2 : ' + str(time.time() - jdst)
    
    raw_input('check : ' + str(join_count) + '-' + str(t1))
    '''
    distance = 1 - (join_count / (combine_count * 1.0))

    return distance
Exemplo n.º 6
0
Arquivo: art.py Projeto: PTCP/PTCP
def mergeIntoCurrentArray(current, newArray):
    if len(current) != len(newArray):
        print 'Error: mergeIntoCurrentArray: length is not equal.'
        raw_input('pause...')
    length = len(current)
    merge_1 = bitarray.to01(bitarray(current) | bitarray(newArray))
    merge = ''
    for i in range(length):
        if newArray[i] == '1':
            merge += newArray[i]
        else:
            merge += current[i]
    if merge != merge_1:
        print 'Error : mergeIntoCurrentArray error...'
        raw_input('mergeIntoCurrentArray error...')
    return merge
Exemplo n.º 7
0
def greedytotal(g_number, test_name, test_cov, test_time, cov_number,
                avg_number):
    # construct initial group - sorted_group, with the group number g_number
    # the coverage of the group is recorded in sorted_coverage
    sorted_group = []
    sorted_coverage = []
    sorted_time = []
    toleratenumber = 0
    global CoverageList
    global CoverageNumber
    global CoverageIndexList
    global TimeList
    global TimeLimit

    TestList = range(len(test_name))
    TimeList = copy.deepcopy(test_time)
    CoverageList = copy.deepcopy(test_cov)

    # construct global variable to record the number of statements in each virtual statement
    CoverageNumber = []
    for i in cov_number:
        CoverageNumber.append(int(i))

    # divide tests into two groups: large_group and small_group.
    # large_group: find the tests that exceed the average execution time for various groups.
    # small_group: find the tests that within the average execution time for various groups.
    large_group, small_group, avg = divideSmallandLarge(
        TestList, g_number, TimeList, avg_number)
    small_number = g_number - len(large_group)
    groupTimeLimit = (avg, (avg_number - 1) * avg)

    # init coverage, sorted_group, sorted_coverage, and sorted_time for allocating tests to different groups.
    init_cov = ''
    for i in range(len(test_cov[0])):
        init_cov += '0'
    for i in range(small_number):
        sorted_group.append([])
        sorted_coverage.append(init_cov)
        sorted_time.append(0)

    # get the descending order of test execution time
    time_sequence = quick_sort_time(small_group)

    # get the order of candidate list based on the descending order of the covered statements per time
    candidate_list = quick_sort(small_group)

    while len(candidate_list) != 0:
        # get the first test in candidate list as candidate test
        candidate_test = candidate_list[0][0]
        candidate_time = TimeList[candidate_test]

        # get the group in which the test execution time is shortest
        # candidate test will be allocated in this candidate group
        candidate_index_list = getIndex(sorted_time, candidate_time,
                                        groupTimeLimit)

        # if the number group in candidate list is 1, this group is candidate group
        # otherwise, the group with max coverage is selected as candidate group
        if len(candidate_index_list) == 1:
            candidate_index = candidate_index_list[0]
        else:
            candidate_index = getMaxCoverage(sorted_coverage,
                                             candidate_index_list)

        # time constraint is checked!
        # 1: if the time constraint is satisfied, the candidate test can be added to candidate group in success
        # 2: if the time constraint is violated, the first test in time_sequence (i.e., decending order of execution time) will be selected as candidate test
        #    and then this test will be added to candidate group.
        cT = checkTime(sorted_group, sorted_time,
                       (candidate_test, candidate_time, candidate_index),
                       time_sequence[0], groupTimeLimit)
        if cT == 1:
            add_name = candidate_test
            add_cov = CoverageList[add_name]
            add_time = TimeList[add_name]
        else:
            add_name = time_sequence[0][0]
            add_cov = CoverageList[add_name]
            add_time = TimeList[add_name]
            toleratenumber += 1

        # add candidate test to candidate group
        # update sorted_group, sorted_time, sorted_coverage
        # remove candidate test from time_sequence and candidate_list
        sorted_group[candidate_index].append(add_name)
        sorted_coverage[candidate_index] = bitarray.to01(
            bitarray(sorted_coverage[candidate_index]) | bitarray(add_cov))
        sorted_time[candidate_index] = sorted_time[candidate_index] + add_time
        for i in range(len(time_sequence)):
            if time_sequence[i][0] == add_name:
                time_sequence.pop(i)
                break
        for i in range(len(candidate_list)):
            if candidate_list[i][0] == add_name:
                candidate_list.pop(i)
                break
    # added the tests in large_group to different group, each group contains only one test
    for item in large_group:
        sorted_group.append([item])
    sorted_group_name = copy.deepcopy(sorted_group)
    # return the name of sorted test.
    for i in range(len(sorted_group_name)):
        for j in range(len(sorted_group_name[i])):
            sorted_group_name[i][j] = test_name[sorted_group[i][j]]
    return sorted_group_name, toleratenumber
Exemplo n.º 8
0
                #print('yes2')
                test_c = '.'.join(test_m.split('.')[0:-1])
            else:
                raw_input('error check..')
            if test_c in testdict.keys():
                testdict[test_c].append(test_index)
            else:
                testdict[test_c] = [test_index]
        f_test = open(subject_path_c + 'testList', 'w')
        f_cov = open(subject_path_c + 'stateMatrix-reduce.txt', 'w')
        f_time = open(subject_path_c + 'exeTime', 'w')
        f_mutant = open(subject_path_c + 'mutantkill-reduce', 'w')
        for test_class in testdict.keys():
            temptime = 0
            tempcov = bitarray('0' * len(cov[0]))
            tempmutant = bitarray('0' * len(mutant[0]))
            f_test.write(test_class + '\n')
            for test_index in testdict[test_class]:
                tempcov = tempcov | bitarray(cov[test_index])
                tempmutant = tempmutant | bitarray(mutant[test_index])
                temptime = temptime + exetime[test_index]
            f_cov.write(bitarray.to01(tempcov) + '\n')
            f_mutant.write(bitarray.to01(tempmutant) + '\n')
            f_time.write(str(temptime) + '\n')
        f_test.close()
        f_cov.close()
        f_mutant.close()
        f_time.close()
        #raw_input(subject + ' check ...')
        print(subject + ' is completed!')