pytorch
146 строк · 4.0 Кб
1
2
3
4
5
6from caffe2.python import core7from hypothesis import given, settings8import caffe2.python.hypothesis_test_util as hu9import caffe2.python.serialized_test.serialized_test_util as serial10import hypothesis.strategies as st11import numpy as np12
13
14def _string_lists(alphabet=None):15return st.lists(16elements=st.text(alphabet=alphabet) if alphabet else st.text(),17min_size=0,18max_size=3)19
20
21class TestStringOps(serial.SerializedTestCase):22@given(strings=_string_lists())23@settings(deadline=10000)24def test_string_prefix(self, strings):25length = 326# although we are utf-8 encoding below to avoid python exceptions,27# StringPrefix op deals with byte-length prefixes, which may produce28# an invalid utf-8 string. The goal here is just to avoid python29# complaining about the unicode -> str conversion.30strings = np.array(31[a.encode('utf-8') for a in strings], dtype=object32)33
34def string_prefix_ref(strings):35return (36np.array([a[:length] for a in strings], dtype=object),37)38
39op = core.CreateOperator(40'StringPrefix',41['strings'],42['stripped'],43length=length)44self.assertReferenceChecks(45hu.cpu_do,46op,47[strings],48string_prefix_ref)49
50@given(strings=_string_lists())51@settings(deadline=10000)52def test_string_suffix(self, strings):53length = 354strings = np.array(55[a.encode('utf-8') for a in strings], dtype=object56)57
58def string_suffix_ref(strings):59return (60np.array([a[-length:] for a in strings], dtype=object),61)62
63op = core.CreateOperator(64'StringSuffix',65['strings'],66['stripped'],67length=length)68self.assertReferenceChecks(69hu.cpu_do,70op,71[strings],72string_suffix_ref)73
74@given(strings=st.text(alphabet=['a', 'b']))75@settings(deadline=10000)76def test_string_starts_with(self, strings):77prefix = 'a'78strings = np.array(79[str(a) for a in strings], dtype=object80)81
82def string_starts_with_ref(strings):83return (84np.array([a.startswith(prefix) for a in strings], dtype=bool),85)86
87op = core.CreateOperator(88'StringStartsWith',89['strings'],90['bools'],91prefix=prefix)92self.assertReferenceChecks(93hu.cpu_do,94op,95[strings],96string_starts_with_ref)97
98@given(strings=st.text(alphabet=['a', 'b']))99@settings(deadline=10000)100def test_string_ends_with(self, strings):101suffix = 'a'102strings = np.array(103[str(a) for a in strings], dtype=object104)105
106def string_ends_with_ref(strings):107return (108np.array([a.endswith(suffix) for a in strings], dtype=bool),109)110
111op = core.CreateOperator(112'StringEndsWith',113['strings'],114['bools'],115suffix=suffix)116self.assertReferenceChecks(117hu.cpu_do,118op,119[strings],120string_ends_with_ref)121
122@given(strings=st.text(alphabet=['a', 'b']))123@settings(deadline=10000)124def test_string_equals(self, strings):125text = ""126if strings:127text = strings[0]128
129strings = np.array(130[str(a) for a in strings], dtype=object131)132
133def string_equals_ref(strings):134return (135np.array([a == text for a in strings], dtype=bool),136)137
138op = core.CreateOperator(139'StringEquals',140['strings'],141['bools'],142text=text)143self.assertReferenceChecks(144hu.cpu_do,145op,146[strings],147string_equals_ref)148
149if __name__ == "__main__":150import unittest151unittest.main()152