common_test.py 3.1 KB
Newer Older
D
dangqingqing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2016 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
import paddle.dataset.common
16 17
import unittest
import tempfile
18
import glob
M
minqiyang 已提交
19
from six.moves import range
20

Y
Yi Wang 已提交
21

22 23
class TestCommon(unittest.TestCase):
    def test_md5file(self):
Y
Yi Wang 已提交
24
        _, temp_path = tempfile.mkstemp()
25 26
        with open(temp_path, 'w') as f:
            f.write("Hello\n")
Y
Yi Wang 已提交
27
        self.assertEqual('09f7e02f1290be211da707a266f153b3',
28
                         paddle.dataset.common.md5file(temp_path))
29

30 31 32
    def test_download(self):
        yi_avatar = 'https://avatars0.githubusercontent.com/u/1548775?v=3&s=460'
        self.assertEqual(
33 34 35
            paddle.dataset.common.DATA_HOME + '/test/1548775?v=3&s=460',
            paddle.dataset.common.download(yi_avatar, 'test',
                                           'f75287202d6622414c706c36c16f8e0d'))
36

37 38 39
    def test_split(self):
        def test_reader():
            def reader():
40
                for x in range(10):
41 42 43 44 45
                    yield x

            return reader

        _, temp_path = tempfile.mkstemp()
46
        paddle.dataset.common.split(
47 48 49 50 51 52
            test_reader(), 4, suffix=temp_path + '/test-%05d.pickle')
        files = glob.glob(temp_path + '/test-%05d.pickle')
        self.assertEqual(len(files), 3)

    def test_cluster_file_reader(self):
        _, temp_path = tempfile.mkstemp()
53
        for x in range(5):
54 55
            with open(temp_path + '/%05d.test' % x) as f:
                f.write('%d\n' % x)
56
        reader = paddle.dataset.common.cluster_files_reader(
57 58 59 60
            temp_path + '/*.test', 5, 0)
        for idx, e in enumerate(reader()):
            self.assertEqual(e, str("0"))

G
gongweibao 已提交
61
    def test_convert(self):
G
gongweibao 已提交
62 63 64
        record_num = 10
        num_shards = 4

G
gongweibao 已提交
65 66
        def test_reader():
            def reader():
67
                for x in range(record_num):
G
gongweibao 已提交
68 69 70 71 72
                    yield x

            return reader

        path = tempfile.mkdtemp()
73 74 75
        paddle.dataset.common.convert(path,
                                      test_reader(), num_shards,
                                      'random_images')
G
gongweibao 已提交
76

G
gongweibao 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        files = glob.glob(path + '/random_images-*')
        self.assertEqual(len(files), num_shards)

        recs = []
        for i in range(0, num_shards):
            n = "%s/random_images-%05d-of-%05d" % (path, i, num_shards - 1)
            r = recordio.reader(n)
            while True:
                d = r.read()
                if d is None:
                    break
                recs.append(d)

        recs.sort()
        self.assertEqual(total, record_num)
G
gongweibao 已提交
92

Y
Yi Wang 已提交
93

94 95
if __name__ == '__main__':
    unittest.main()