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