Exemple #1
0
 def test_join_ok(self) -> None:
     def f(items: List[str]) -> str:
         '''
         post: True
         '''
         return ', '.join(items)
     self.assertEqual(*check_unknown(f))
Exemple #2
0
 def test_union_ok(self) -> None:
     def f(a: Set[str], b: Set[str]) -> Set[str]:
         '''
         post: all(((i in a) or (i in b)) for i in _)
         '''
         return a | b
     self.assertEqual(*check_unknown(f))
Exemple #3
0
    def test_upper_unknown(self) -> None:
        def f(s: str) -> str:
            """ post: __return__ != "FOOBAR" """
            return s.upper()

        self.assertEqual(*check_unknown(
            f))  # Ideally we'd find the counterexample input, "foobar"
Exemple #4
0
    def test_hashable_values_ok(self) -> None:
        def f(a: Tuple[str, int, float, bool], b: Tuple[str, int, float,
                                                        bool]) -> int:
            """ post: _ or not (a == b) """
            return hash(a) == hash(b)

        self.assertEqual(*check_unknown(f))
 def test_full_symbolic_multiply_unknown(self) -> None:
     def f(s: str, i: int) -> str:
         '''
         pre: s and i > 0
         post: _[0] == s[0]
         '''
         return s * i
     self.assertEqual(*check_unknown(f))
 def test_round_unknown(self) -> None:
     def f(num: float, ndigits: Optional[int]) -> float:
         '''
         post: isinstance(_, int) == (ndigits is None)
         '''
         return round(num, ndigits)
     # TODO: this is unknown (rounding reals is hard)
     self.assertEqual(*check_unknown(f))
 def test_symbolic_dict_has_unique_keys(self) -> None:
     def f(d: Dict[Tuple[int, str], int]) -> None:
         '''
         pre: (1, 'one') in d
         post[d]: (1, 'one') not in d
         '''
         del d[(1, 'one')]
     self.assertEqual(*check_unknown(f))
Exemple #8
0
 def test_max_ok(self) -> None:
     def f(l: List[int]) -> int:
         '''
         pre: bool(l)
         post[]: _ in l
         '''
         return max(l)
     self.assertEqual(*check_unknown(f))
Exemple #9
0
 def test_slice_outside_range_ok(self) -> None:
     def f(l: List[int], i: int) -> List[int]:
         '''
         pre: i >= len(l)
         post: _ == l
         '''
         return l[:i]
     self.assertEqual(*check_unknown(f))
Exemple #10
0
 def test_round_unknown(self) -> None:
     def f(num: float, ndigits: Optional[int]) -> float:
         '''
         post: isinstance(_, int) == (ndigits is None)
         '''
         return round(num, ndigits)
     # TODO: this is unknown (z3 can't solve 10**x != 0 right now)
     self.assertEqual(*check_unknown(f))
    def test_repr_equiv(self) -> None:
        def f(symbolic: DefaultDict[int, int]) -> Tuple[dict, dict]:
            """ post: _[0] == _[1] """
            concrete = collections.defaultdict(symbolic.default_factory,
                                               symbolic.items())
            return (symbolic, concrete)

        self.assertEqual(*check_unknown(f))
Exemple #12
0
    def test_realized_compare(self) -> None:
        def f(a: str, b: str) -> bool:
            """
            post: implies(_, a == b)
            """
            return realize(a) == b

        self.assertEqual(*check_unknown(f))
Exemple #13
0
 def test_percent_format(self) -> None:
     def f(fmt: str) -> str:
         '''
         pre: '%' not in fmt
         post: True
         '''
         return fmt % ()
     self.assertEqual(*check_unknown(f))
 def test_contains_different_but_equivalent(self) -> None:
     def f(s: Set[Union[int, str]]) -> None:
         '''
         pre: "foobar" in s
         post: (_ + "bar") in s
         '''
         return "foo"
     self.assertEqual(*check_unknown(f))
Exemple #15
0
 def test_min_ok(self) -> None:
     def f(l: List[float]) -> float:
         '''
         pre: bool(l)
         post[]: _ in l
         '''
         return min(l)
     self.assertEqual(*check_unknown(f))
Exemple #16
0
 def test_average(self) -> None:
     def average(numbers: List[float]) -> float:
         '''
         pre: len(numbers) > 0
         post: min(numbers) <= _ <= max(numbers)
         '''
         return sum(numbers) / len(numbers)
     self.assertEqual(*check_unknown(average))
    def test_plus_unknown(self) -> None:
        def f(s: str) -> bool:
            """
            pre: len(s) > 0
            post: _
            """
            return bool(re.fullmatch(".+", s, re.DOTALL))

        self.assertEqual(*check_unknown(f))
Exemple #18
0
    def test_slice_assignment_out_of_bounds(self) -> None:
        def f(l: List[int], i: int) -> None:
            """
            pre: i != -1
            post: l == __old__.l[:i] + __old__.l[i+1:]
            """
            l[i:i + 1] = []

        self.assertEqual(*check_unknown(f))
 def test_iterable(self) -> None:
     T = TypeVar('T')
     def f(a: Iterable[T]) -> T:
         '''
         pre: a
         post: _ in a
         '''
         return next(iter(a))
     self.assertEqual(*check_unknown(f))
    def test_date_plus_delta_unknown(self) -> None:
        def f(delta: datetime.timedelta) -> datetime.date:
            """
            post: _.year != -9999
            raises: OverflowError
            """
            return datetime.date(2000, 1, 1) + delta

        self.assertEqual(*check_unknown(f))
Exemple #21
0
    def test_plus_unknown(self) -> None:
        def f(s: str) -> bool:
            '''
            pre: len(s) > 0
            post: _
            '''
            return bool(re.fullmatch('.+', s, re.DOTALL))

        self.assertEqual(*check_unknown(f))
Exemple #22
0
 def test_csv_example(self) -> None:
     def f(lines: List[str]) -> List[str]:
         '''
         pre: all(',' in line for line in lines)
         post: __return__ == [line.split(',')[0] for line in lines]
         '''
         return [line[:line.index(',')] for line in lines]
     # TODO: the model generation doesn't work right here (getting a lot of empty strings):
     self.assertEqual(*check_unknown(f))
Exemple #23
0
 def test_extend_literal_unknown(self) -> None:
     def f(l: List[int]) -> List[int]:
         '''
         post: _[:2] == [1, 2]
         '''
         r = [1, 2, 3]
         r.extend(l)
         return r
     self.assertEqual(*check_unknown(f))
Exemple #24
0
    def test_callable_with_typevar_in_return(self) -> None:
        # For now, just don't explode. But we should be able to make these fail with
        # some work. See https://github.com/pschanely/CrossHair/issues/85
        T = TypeVar("T")

        def f(a: Callable[[int], T], x: int) -> T:
            """post: _"""
            return a(x)

        self.assertEqual(*check_unknown(f))
Exemple #25
0
    def test_csv_example(self) -> None:
        def f(lines: List[str]) -> List[str]:
            """
            pre: all(',' in line for line in lines)
            post: __return__ == [line.split(',')[0] for line in lines]
            """
            return [line[:line.index(",")] for line in lines]

        # TODO: the model generation doesn't work right here (getting a lot of empty strings):
        options = AnalysisOptionSet(per_path_timeout=0.5,
                                    per_condition_timeout=5)
        self.assertEqual(*check_unknown(f, options))
Exemple #26
0
    def test_unrelated_regex(self) -> None:
        def f(s: str) -> bool:
            """ post: True """
            return bool(re.match(r"(\d+)", s))

        self.assertEqual(*check_unknown(f))
Exemple #27
0
    def test_equality_ok(self) -> None:
        def f(d: Dict[int, int]) -> Dict[int, int]:
            """ post: _ == {**_} """
            return d

        self.assertEqual(*check_unknown(f))
Exemple #28
0
 def test_range_can_be_called(self) -> None:
     def f(a: int) -> Iterable[int]:
         ''' post: len(_) == a or a < 0 '''
         return range(a)
     self.assertEqual(*check_unknown(f))
Exemple #29
0
    def test_consistent_ordering(self) -> None:
        def f(symbolic: Dict[int, int]) -> Tuple[List[int], List[int]]:
            """ post: _[0] == _[1] """
            return (list(symbolic.keys()), list(symbolic.keys()))

        self.assertEqual(*check_unknown(f))
Exemple #30
0
    def test_fallback_when_smt_values_out_themselves(self) -> None:
        def f(items: List[str]) -> str:
            """ post: True """
            return ",".join(items)

        self.assertEqual(*check_unknown(f))