예제 #1
0
class TestCase(Case):
    list1 = IntArray(0, 10**2, IntVar(1, 5))
    list2 = FloatArray(0, 10**2, IntVar(1, 5))

    def __inp_str__(self):
        return f"{list_printer(self.list1, sep=' ')}\n{list_printer(self.list2, sep=' ')}"

    def config(self):
        self.app = r'./my_min.py'
예제 #2
0
class TestCase(Case):
    len_str_1 = IntVar(0, 50)
    len_str_2 = IntVar(0, 50)
    str_1 = CharArray(len_str_1)
    str_2 = CharArray(len_str_2)

    def __inp_str__(self):
        return f"{string_printer(self.str_1)}\n{string_printer(self.str_2)}"

    def config(self):
        self.function = lcs
        self.input_sequence = [
            self.str_1, self.str_2, self.len_str_1, self.len_str_2
        ]
예제 #3
0
class IntArrayBoundingTest(Case, ):
    tc = unittest.TestCase()

    batch_size = 1000
    a = IntVar(1, 100)
    b = IntVar(a, 500)
    arr = IntArray(a, b, 20)

    def __otp_str__(self) -> str:
        return ""

    def config(self):
        self.input_sequence = [self.a, self.b, self.arr]
        self.function = lambda a, b, arr: self.tc.assertTrue(min(arr) >= a and max(arr) <= b)
        self.writer = mock_writer
예제 #4
0
class VariableBoundingTest(
        Case, ):
    tc = unittest.TestCase()

    batch_size = 1000
    a = IntVar(0, 100)
    b = IntVar(a, 200)
    c = FloatVar(a, b)

    def __otp_str__(self) -> str:
        return ""

    def config(self):
        self.input_sequence = [self.a, self.b, self.c]
        self.function = lambda a, b, c: self.tc.assertTrue(
            a <= c <= b, f"{a}, {b}, {c}")
        self.writer = mock_writer
예제 #5
0
class ArraySizeTest(Case, ):
    tc = unittest.TestCase()

    batch_size = 1000
    n = IntVar(1, 1000)
    arr = IntArray(0, 5, n)

    def __otp_str__(self) -> str:
        return ""

    def config(self):
        self.input_sequence = [self.n, self.arr]
        self.function = lambda n, arr: self.tc.assertEqual(len(arr), n)
        self.writer = mock_writer
예제 #6
0
class CustomArrayGenTest(Case, ):
    tc = unittest.TestCase()

    batch_size = 1000
    a = FloatVar(1, 100.42)
    b = FloatVar(a, 500.1)

    @staticmethod
    def gen(f1, f2):
        while True:
            yield f1 + f2

    n = IntVar(1, 100)
    arr = CustomArray(n, gen.__get__(object, object), a, b)

    def __otp_str__(self) -> str:
        return ""

    def config(self):
        self.input_sequence = [self.arr, self.a, self.b]
        self.function = lambda arr, f1, f2: self.tc.assertEqual(min(arr), max(arr), f1 + f2)
        self.writer = mock_writer
예제 #7
0
class TestCase(Case):
    # defines how many test cases will be generated
    batch_size = 1

    # variables:

    # single Integer Variable
    n = IntVar(1, 10)
    m = IntVar(1, 10)

    # Array of Integer with given size and bounds
    # you can use Variables in arguments of variables constructors too!
    arr = IntArray(lower_bound=0, upper_bound=100, length=n)
    # single float Variable
    f1 = FloatVar(1, 10, decimal_places=2)
    f2 = FloatVar(f1, 10, decimal_places=2)

    # Array of Integer with given size and bounds
    float_arr = FloatArray(lower_bound=f1,
                           upper_bound=f2,
                           length=n,
                           decimal_places=3)

    # An array from Choice List with given size
    letters = ChoiceList(length=n, choice_list=string.ascii_uppercase)

    # An array from your custom generator function.
    # args after generator will be passed to generator.
    gamma_gen = CustomArray(n, custom_generator, f1, f2, n)

    # 2d Array from any type of array.
    # making 2d array from a 2d array will produce a 3d array
    # and so on.
    arr2d = Array2d(array=gamma_gen, length=m)

    arr3d = Array2d(array=arr2d, length=m)

    # defines how inputs will be printed to the writer
    def __inp_str__(self):
        return f"""
n           : {self.n}
m           : {self.m}

arr[{self.n}]      : {self.arr}
arr[{self.n}]      : {list_printer(self.arr, sep=" ")}

f1, f2      : {self.f1}, {self.f2}

float_arr[{self.n}]: {self.float_arr}

letters[{self.n}]  : {list_printer(self.letters, sep="")}
gamma_gen[{self.n}]: {self.gamma_gen}

arr2d[{self.n}][{self.m}] :
{list2d_printer(self.arr2d)}

        """

    # defines how outputs will be printed to the writer
    def __otp_str__(self) -> str:
        return f"output: \n{self.output}"

    def config(self):
        # if u want generate output, you have 2 options
        #
        # 1: using a function; just set self.function
        # here we will use builtin min function
        # that return minimum value of a list
        self.function = min
        # then set your functions input arguments
        # here we will pass both arr and float_arr to the min function
        self.input_sequence = [[*self.arr, *self.float_arr]]

        # or
        # 2: using a python application.
        # you can generate output with your python program.
        # just set your application path like
        # self.app = r'my_min.py'
        # and pycontest will use __inp_str__
        # to generate the input for your program
        # and mock it as std.in to your program.
        # see https://github.com/matinhimself/pycontest/blob/main/examples/example_app.py

        # Default writer, writes each testcase
        # into separate files like below:
        # └───tests
        #     ├────in
        #     │     ├───input1.txt
        #     │     ├───input2.txt
        #     │     │  . . .
        #     │     └───input10.txt
        #     └────out
        #           ├───output1.txt
        #           ├───output2.txt
        #           │  . . .
        #           └───output10.txt
        #
        # + You can customize directory names
        #   , input/output and ... of default writer helper.OutputHelper.
        #       self.writer = OutputHelper(kwargs)
        #
        # - Make your own writer class implementing
        #     writer.OutputWriter Protocol
        #
        # - If you want to print all testCases
        #   in terminal:
        #       self.writer = sys.stdout
        #
        # - If you want all test cases in a file:
        #       self.writer = open("file.txt","a")

        # sys.redirect_stdout will close
        # io automatically after printing
        #
        # self.writer = open("tests/test.txt", "a")

        self.writer = sys.stdout