test_compat.py 19.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15 16
from __future__ import print_function

17
import unittest
M
minqiyang 已提交
18
import paddle.compat as cpt
19 20 21 22
import six


class TestCompatible(unittest.TestCase):
M
minqiyang 已提交
23 24 25 26 27 28 29 30
    def test_type(self):
        if six.PY2:
            self.assertEqual(cpt.int_type, int)
            self.assertEqual(cpt.long_type, long)
        else:
            self.assertEqual(cpt.int_type, int)
            self.assertEqual(cpt.long_type, int)

M
minqiyang 已提交
31
    def test_to_text(self):
32 33 34 35 36
        # Only support python2.x and python3.x now
        self.assertTrue(six.PY2 | six.PY3)

        if six.PY2:
            # check None
M
minqiyang 已提交
37
            self.assertIsNone(cpt.to_text(None))
38 39

            # check all string related types
M
minqiyang 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52
            self.assertTrue(isinstance(cpt.to_text(str("")), unicode))
            self.assertTrue(isinstance(cpt.to_text(str("123")), unicode))
            self.assertTrue(isinstance(cpt.to_text(b""), unicode))
            self.assertTrue(isinstance(cpt.to_text(b""), unicode))
            self.assertTrue(isinstance(cpt.to_text(u""), unicode))
            self.assertTrue(isinstance(cpt.to_text(u""), unicode))

            self.assertEqual(u"", cpt.to_text(str("")))
            self.assertEqual(u"123", cpt.to_text(str("123")))
            self.assertEqual(u"", cpt.to_text(b""))
            self.assertEqual(u"123", cpt.to_text(b"123"))
            self.assertEqual(u"", cpt.to_text(u""))
            self.assertEqual(u"123", cpt.to_text(u"123"))
53 54 55

            # check list types, not inplace
            l = [""]
M
minqiyang 已提交
56
            l2 = cpt.to_text(l)
57 58 59 60 61
            self.assertTrue(isinstance(l2, list))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([u""], l2)
            l = ["", "123"]
M
minqiyang 已提交
62
            l2 = cpt.to_text(l)
63 64 65 66 67
            self.assertTrue(isinstance(l2, list))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([u"", u"123"], l2)
            l = ["", b'123', u"321"]
M
minqiyang 已提交
68
            l2 = cpt.to_text(l)
69 70 71 72 73 74 75 76 77
            self.assertTrue(isinstance(l2, list))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([u"", u"123", u"321"], l2)
            for i in l2:
                self.assertTrue(isinstance(i, unicode))

            # check list types, inplace
            l = [""]
M
minqiyang 已提交
78
            l2 = cpt.to_text(l, inplace=True)
79 80 81 82 83
            self.assertTrue(isinstance(l2, list))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([u""], l2)
            l = ["", "123"]
M
minqiyang 已提交
84
            l2 = cpt.to_text(l, inplace=True)
85 86 87 88 89
            self.assertTrue(isinstance(l2, list))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([u"", u"123"], l2)
            l = ["", b"123", u"321"]
M
minqiyang 已提交
90
            l2 = cpt.to_text(l, inplace=True)
91 92 93 94 95 96 97
            self.assertTrue(isinstance(l2, list))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([u"", u"123", u"321"], l2)

            # check set types, not inplace
            l = set("")
M
minqiyang 已提交
98
            l2 = cpt.to_text(l, inplace=False)
99 100 101 102 103
            self.assertTrue(isinstance(l2, set))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set(u""), l2)
            l = set([b"", b"123"])
M
minqiyang 已提交
104
            l2 = cpt.to_text(l, inplace=False)
105 106 107 108 109
            self.assertTrue(isinstance(l2, set))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set([u"", u"123"]), l2)
            l = set(["", b"123", u"321"])
M
minqiyang 已提交
110
            l2 = cpt.to_text(l, inplace=False)
111 112 113 114 115 116 117 118 119
            self.assertTrue(isinstance(l2, set))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set([u"", u"123", u"321"]), l2)
            for i in l2:
                self.assertTrue(isinstance(i, unicode))

            # check set types, inplace
            l = set("")
M
minqiyang 已提交
120
            l2 = cpt.to_text(l, inplace=True)
121 122 123 124 125
            self.assertTrue(isinstance(l2, set))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set(u""), l2)
            l = set([b"", b"123"])
M
minqiyang 已提交
126
            l2 = cpt.to_text(l, inplace=True)
127 128 129 130 131
            self.assertTrue(isinstance(l2, set))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set([u"", u"123"]), l2)
            l = set(["", b"123", u"321"])
M
minqiyang 已提交
132
            l2 = cpt.to_text(l, inplace=True)
133 134 135 136 137 138
            self.assertTrue(isinstance(l2, set))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set([u"", u"123", u"321"]), l2)

        elif six.PY3:
M
minqiyang 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
            self.assertIsNone(cpt.to_text(None))

            self.assertTrue(isinstance(cpt.to_text(str("")), str))
            self.assertTrue(isinstance(cpt.to_text(str("123")), str))
            self.assertTrue(isinstance(cpt.to_text(b""), str))
            self.assertTrue(isinstance(cpt.to_text(b""), str))
            self.assertTrue(isinstance(cpt.to_text(u""), str))
            self.assertTrue(isinstance(cpt.to_text(u""), str))

            self.assertEqual("", cpt.to_text(str("")))
            self.assertEqual("123", cpt.to_text(str("123")))
            self.assertEqual("", cpt.to_text(b""))
            self.assertEqual("123", cpt.to_text(b"123"))
            self.assertEqual("", cpt.to_text(u""))
            self.assertEqual("123", cpt.to_text(u"123"))
154 155 156

            # check list types, not inplace
            l = [""]
M
minqiyang 已提交
157
            l2 = cpt.to_text(l)
158 159 160 161 162
            self.assertTrue(isinstance(l2, list))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([""], l2)
            l = ["", "123"]
M
minqiyang 已提交
163
            l2 = cpt.to_text(l)
164 165 166 167 168
            self.assertTrue(isinstance(l2, list))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(["", "123"], l2)
            l = ["", b"123", u"321"]
M
minqiyang 已提交
169
            l2 = cpt.to_text(l)
170 171 172 173 174 175 176
            self.assertTrue(isinstance(l2, list))
            self.assertFalse(l is l2)
            self.assertNotEqual(l, l2)
            self.assertEqual(["", "123", "321"], l2)

            # check list types, inplace
            l = [""]
M
minqiyang 已提交
177
            l2 = cpt.to_text(l, inplace=True)
178 179 180 181 182
            self.assertTrue(isinstance(l2, list))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([""], l2)
            l = ["", b"123"]
M
minqiyang 已提交
183
            l2 = cpt.to_text(l, inplace=True)
184 185 186 187 188
            self.assertTrue(isinstance(l2, list))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(["", "123"], l2)
            l = ["", b"123", u"321"]
M
minqiyang 已提交
189
            l2 = cpt.to_text(l, inplace=True)
190 191 192 193 194 195 196 197 198
            self.assertTrue(isinstance(l2, list))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(["", "123", "321"], l2)
            for i in l2:
                self.assertTrue(isinstance(i, str))

            # check set types, not inplace
            l = set("")
M
minqiyang 已提交
199
            l2 = cpt.to_text(l, inplace=False)
200 201 202 203 204
            self.assertTrue(isinstance(l2, set))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set(""), l2)
            l = set([b"", b"123"])
M
minqiyang 已提交
205
            l2 = cpt.to_text(l, inplace=False)
206 207 208 209 210
            self.assertTrue(isinstance(l2, set))
            self.assertFalse(l is l2)
            self.assertNotEqual(l, l2)
            self.assertEqual(set(["", "123"]), l2)
            l = set(["", b"123", u"321"])
M
minqiyang 已提交
211
            l2 = cpt.to_text(l, inplace=False)
212 213 214 215 216 217 218
            self.assertTrue(isinstance(l2, set))
            self.assertFalse(l is l2)
            self.assertNotEqual(l, l2)
            self.assertEqual(set(["", "123", "321"]), l2)

            # check set types, inplace
            l = set("")
M
minqiyang 已提交
219
            l2 = cpt.to_text(l, inplace=True)
220 221 222 223 224
            self.assertTrue(isinstance(l2, set))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set(""), l2)
            l = set([b"", b"123"])
M
minqiyang 已提交
225
            l2 = cpt.to_text(l, inplace=True)
226 227 228 229 230
            self.assertTrue(isinstance(l2, set))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set(["", "123"]), l2)
            l = set(["", b"123", u"321"])
M
minqiyang 已提交
231
            l2 = cpt.to_text(l, inplace=True)
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
            self.assertTrue(isinstance(l2, set))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set(["", "123", "321"]), l2)
            for i in l2:
                self.assertTrue(isinstance(i, str))

    def test_to_bytes(self):
        # Only support python2.x and python3.x now
        self.assertTrue(six.PY2 | six.PY3)

        if six.PY2:
            # check None
            self.assertIsNone(cpt.to_bytes(None))

            # check all string related types
            self.assertTrue(isinstance(cpt.to_bytes(str("")), bytes))
            self.assertTrue(isinstance(cpt.to_bytes(str("123")), bytes))
            self.assertTrue(isinstance(cpt.to_bytes(b""), bytes))
            self.assertTrue(isinstance(cpt.to_bytes(b""), bytes))
            self.assertTrue(isinstance(cpt.to_bytes(u""), bytes))
            self.assertTrue(isinstance(cpt.to_bytes(u""), bytes))

            self.assertEqual(b"", cpt.to_bytes(str("")))
            self.assertEqual(b"123", cpt.to_bytes(str("123")))
            self.assertEqual(b"", cpt.to_bytes(b""))
            self.assertEqual(b"123", cpt.to_bytes(b"123"))
            self.assertEqual(b"", cpt.to_bytes(u""))
            self.assertEqual(b"123", cpt.to_bytes(u"123"))

            # check list types, not inplace
            l = [""]
            l2 = cpt.to_bytes(l)
            self.assertTrue(isinstance(l2, list))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([b""], l2)
            l = ["", "123"]
            l2 = cpt.to_bytes(l)
            self.assertTrue(isinstance(l2, list))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([b"", b"123"], l2)
            l = ["", b'123', u"321"]
            l2 = cpt.to_bytes(l)
            self.assertTrue(isinstance(l2, list))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([b"", b"123", b"321"], l2)
            for i in l2:
                self.assertTrue(isinstance(i, bytes))

            # check list types, inplace
            l = [""]
            l2 = cpt.to_bytes(l, inplace=True)
            self.assertTrue(isinstance(l2, list))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([b""], l2)
            l = ["", "123"]
            l2 = cpt.to_bytes(l, inplace=True)
            self.assertTrue(isinstance(l2, list))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([b"", b"123"], l2)
            l = ["", b"123", u"321"]
            l2 = cpt.to_bytes(l, inplace=True)
            self.assertTrue(isinstance(l2, list))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([b"", b"123", b"321"], l2)

            # check set types, not inplace
            l = set("")
            l2 = cpt.to_bytes(l, inplace=False)
            self.assertTrue(isinstance(l2, set))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set(b""), l2)
            l = set([b"", b"123"])
            l2 = cpt.to_bytes(l, inplace=False)
            self.assertTrue(isinstance(l2, set))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set([b"", b"123"]), l2)
            l = set(["", b"123", u"321"])
            l2 = cpt.to_bytes(l, inplace=False)
            self.assertTrue(isinstance(l2, set))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set([b"", b"123", b"321"]), l2)
            for i in l2:
                self.assertTrue(isinstance(i, bytes))

            # check set types, inplace
            l = set("")
            l2 = cpt.to_bytes(l, inplace=True)
            self.assertTrue(isinstance(l2, set))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set(b""), l2)
            l = set([b"", b"123"])
            l2 = cpt.to_bytes(l, inplace=True)
            self.assertTrue(isinstance(l2, set))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set([b"", b"123"]), l2)
            l = set(["", b"123", u"321"])
            l2 = cpt.to_bytes(l, inplace=True)
            self.assertTrue(isinstance(l2, set))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set([b"", b"123", b"321"]), l2)

        elif six.PY3:
            self.assertIsNone(cpt.to_bytes(None))

            self.assertTrue(isinstance(cpt.to_bytes(str("")), bytes))
            self.assertTrue(isinstance(cpt.to_bytes(str("123")), bytes))
            self.assertTrue(isinstance(cpt.to_bytes(b""), bytes))
            self.assertTrue(isinstance(cpt.to_bytes(b""), bytes))
            self.assertTrue(isinstance(cpt.to_bytes(u""), bytes))
            self.assertTrue(isinstance(cpt.to_bytes(u""), bytes))

            self.assertEqual(b"", cpt.to_bytes(str("")))
            self.assertEqual(b"123", cpt.to_bytes(str("123")))
            self.assertEqual(b"", cpt.to_bytes(b""))
            self.assertEqual(b"123", cpt.to_bytes(b"123"))
            self.assertEqual(b"", cpt.to_bytes(u""))
            self.assertEqual(b"123", cpt.to_bytes(u"123"))

            # check list types, not inplace
            l = [""]
            l2 = cpt.to_bytes(l)
            self.assertTrue(isinstance(l2, list))
            self.assertFalse(l is l2)
            self.assertNotEqual(l, l2)
            self.assertEqual([b""], l2)
            l = ["", "123"]
            l2 = cpt.to_bytes(l)
            self.assertTrue(isinstance(l2, list))
            self.assertFalse(l is l2)
            self.assertNotEqual(l, l2)
            self.assertEqual([b"", b"123"], l2)
            l = ["", b"123", u"321"]
            l2 = cpt.to_bytes(l)
            self.assertTrue(isinstance(l2, list))
            self.assertFalse(l is l2)
            self.assertNotEqual(l, l2)
            self.assertEqual([b"", b"123", b"321"], l2)

            # check list types, inplace
            l = [""]
            l2 = cpt.to_bytes(l, inplace=True)
            self.assertTrue(isinstance(l2, list))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([b""], l2)
            l = ["", b"123"]
            l2 = cpt.to_bytes(l, inplace=True)
            self.assertTrue(isinstance(l2, list))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([b"", b"123"], l2)
            l = ["", b"123", u"321"]
            l2 = cpt.to_bytes(l, inplace=True)
            self.assertTrue(isinstance(l2, list))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([b"", b"123", b"321"], l2)
            for i in l2:
                self.assertTrue(isinstance(i, bytes))

            # check set types, not inplace
            l = set([""])
            l2 = cpt.to_bytes(l, inplace=False)
            self.assertTrue(isinstance(l2, set))
            self.assertFalse(l is l2)
            self.assertNotEqual(l, l2)
            self.assertEqual(set([b""]), l2)
            l = set([u"", u"123"])
            l2 = cpt.to_bytes(l, inplace=False)
            self.assertTrue(isinstance(l2, set))
            self.assertFalse(l is l2)
            self.assertNotEqual(l, l2)
            self.assertEqual(set([b"", b"123"]), l2)
            l = set(["", b"123", u"321"])
            l2 = cpt.to_bytes(l, inplace=False)
            self.assertTrue(isinstance(l2, set))
            self.assertFalse(l is l2)
            self.assertNotEqual(l, l2)
            self.assertEqual(set([b"", b"123", b"321"]), l2)

            # check set types, inplace
            l = set("")
            l2 = cpt.to_bytes(l, inplace=True)
            self.assertTrue(isinstance(l2, set))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set(b""), l2)
            l = set([u"", u"123"])
            l2 = cpt.to_bytes(l, inplace=True)
            self.assertTrue(isinstance(l2, set))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set([b"", b"123"]), l2)
            l = set(["", b"123", u"321"])
            l2 = cpt.to_bytes(l, inplace=True)
            self.assertTrue(isinstance(l2, set))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual(set([b"", b"123", b"321"]), l2)
            for i in l2:
                self.assertTrue(isinstance(i, bytes))

    def test_round(self):
        self.assertEqual(3.0, cpt.round(3.4))
        self.assertEqual(4.0, cpt.round(3.5))
        self.assertEqual(0.0, cpt.round(0.1))
M
minqiyang 已提交
451 452
        self.assertEqual(0.0, cpt.round(0.0))
        self.assertEqual(-0.0, cpt.round(-0.0))
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
        self.assertEqual(-0.0, cpt.round(-0.1))
        self.assertEqual(-3.0, cpt.round(-3.4))
        self.assertEqual(-4.0, cpt.round(-3.5))
        self.assertEqual(5.0, cpt.round(5))
        self.assertRaises(TypeError, cpt.round, None)

    def test_floor_division(self):
        self.assertEqual(0.0, cpt.floor_division(3, 4))
        self.assertEqual(1.0, cpt.floor_division(4, 3))
        self.assertEqual(2.0, cpt.floor_division(6, 3))
        self.assertEqual(-2.0, cpt.floor_division(-4, 3))
        self.assertEqual(-2.0, cpt.floor_division(-6, 3))
        self.assertRaises(ZeroDivisionError, cpt.floor_division, 3, 0)
        self.assertRaises(TypeError, cpt.floor_division, None, None)

    def test_get_exception_message(self):
        exception_message = "test_message"
        self.assertRaises(AssertionError, cpt.get_exception_message, None)
        if six.PY2:
M
minqiyang 已提交
472 473
            self.assertRaises(AttributeError, cpt.get_exception_message,
                              exception_message)
474 475 476
            try:
                raise RuntimeError(exception_message)
            except Exception as e:
M
minqiyang 已提交
477 478
                self.assertEqual(exception_message,
                                 cpt.get_exception_message(e))
479 480 481 482 483
                self.assertIsNotNone(e)

            try:
                raise Exception(exception_message)
            except Exception as e:
M
minqiyang 已提交
484 485
                self.assertEqual(exception_message,
                                 cpt.get_exception_message(e))
486 487 488 489 490 491
                self.assertIsNotNone(e)

        if six.PY3:
            try:
                raise RuntimeError(exception_message)
            except Exception as e:
M
minqiyang 已提交
492 493
                self.assertEqual(exception_message,
                                 cpt.get_exception_message(e))
494 495 496 497 498
                self.assertIsNotNone(e)

            try:
                raise Exception(exception_message)
            except Exception as e:
M
minqiyang 已提交
499 500
                self.assertEqual(exception_message,
                                 cpt.get_exception_message(e))
501 502 503 504 505
                self.assertIsNotNone(e)


if __name__ == "__main__":
    unittest.main()