test_compat.py 19.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#   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.

import unittest
import paddle.fluid.compat as cpt
import six


class TestCompatible(unittest.TestCase):
M
minqiyang 已提交
21 22 23 24 25 26 27 28
    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 已提交
29
    def test_to_text(self):
30 31 32 33 34
        # Only support python2.x and python3.x now
        self.assertTrue(six.PY2 | six.PY3)

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

            # check all string related types
M
minqiyang 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50
            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"))
51 52 53

            # check list types, not inplace
            l = [""]
M
minqiyang 已提交
54
            l2 = cpt.to_text(l)
55 56 57 58 59
            self.assertTrue(isinstance(l2, list))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([u""], l2)
            l = ["", "123"]
M
minqiyang 已提交
60
            l2 = cpt.to_text(l)
61 62 63 64 65
            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 已提交
66
            l2 = cpt.to_text(l)
67 68 69 70 71 72 73 74 75
            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 已提交
76
            l2 = cpt.to_text(l, inplace=True)
77 78 79 80 81
            self.assertTrue(isinstance(l2, list))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([u""], l2)
            l = ["", "123"]
M
minqiyang 已提交
82
            l2 = cpt.to_text(l, inplace=True)
83 84 85 86 87
            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 已提交
88
            l2 = cpt.to_text(l, inplace=True)
89 90 91 92 93 94 95
            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 已提交
96
            l2 = cpt.to_text(l, inplace=False)
97 98 99 100 101
            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 已提交
102
            l2 = cpt.to_text(l, inplace=False)
103 104 105 106 107
            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 已提交
108
            l2 = cpt.to_text(l, inplace=False)
109 110 111 112 113 114 115 116 117
            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 已提交
118
            l2 = cpt.to_text(l, inplace=True)
119 120 121 122 123
            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 已提交
124
            l2 = cpt.to_text(l, inplace=True)
125 126 127 128 129
            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 已提交
130
            l2 = cpt.to_text(l, inplace=True)
131 132 133 134 135 136
            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 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
            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"))
152 153 154

            # check list types, not inplace
            l = [""]
M
minqiyang 已提交
155
            l2 = cpt.to_text(l)
156 157 158 159 160
            self.assertTrue(isinstance(l2, list))
            self.assertFalse(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([""], l2)
            l = ["", "123"]
M
minqiyang 已提交
161
            l2 = cpt.to_text(l)
162 163 164 165 166
            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 已提交
167
            l2 = cpt.to_text(l)
168 169 170 171 172 173 174
            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 已提交
175
            l2 = cpt.to_text(l, inplace=True)
176 177 178 179 180
            self.assertTrue(isinstance(l2, list))
            self.assertTrue(l is l2)
            self.assertEqual(l, l2)
            self.assertEqual([""], l2)
            l = ["", b"123"]
M
minqiyang 已提交
181
            l2 = cpt.to_text(l, inplace=True)
182 183 184 185 186
            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 已提交
187
            l2 = cpt.to_text(l, inplace=True)
188 189 190 191 192 193 194 195 196
            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 已提交
197
            l2 = cpt.to_text(l, inplace=False)
198 199 200 201 202
            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 已提交
203
            l2 = cpt.to_text(l, inplace=False)
204 205 206 207 208
            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 已提交
209
            l2 = cpt.to_text(l, inplace=False)
210 211 212 213 214 215 216
            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 已提交
217
            l2 = cpt.to_text(l, inplace=True)
218 219 220 221 222
            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 已提交
223
            l2 = cpt.to_text(l, inplace=True)
224 225 226 227 228
            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 已提交
229
            l2 = cpt.to_text(l, inplace=True)
230 231 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
            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 已提交
449 450
        self.assertEqual(0.0, cpt.round(0.0))
        self.assertEqual(-0.0, cpt.round(-0.0))
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
        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 已提交
470 471
            self.assertRaises(AttributeError, cpt.get_exception_message,
                              exception_message)
472 473 474
            try:
                raise RuntimeError(exception_message)
            except Exception as e:
M
minqiyang 已提交
475 476
                self.assertEqual(exception_message,
                                 cpt.get_exception_message(e))
477 478 479 480 481
                self.assertIsNotNone(e)

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

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

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


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