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 16
from __future__ import print_function

17
import paddle.dataset.common
18 19
import unittest
import tempfile
20
import glob
M
minqiyang 已提交
21
from six.moves import range
22

Y
Yi Wang 已提交
23

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

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

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

            return reader

        _, temp_path = tempfile.mkstemp()
48
        paddle.dataset.common.split(
49 50 51 52 53 54
            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()
55
        for x in range(5):
56 57
            with open(temp_path + '/%05d.test' % x) as f:
                f.write('%d\n' % x)
58
        reader = paddle.dataset.common.cluster_files_reader(
59 60 61 62
            temp_path + '/*.test', 5, 0)
        for idx, e in enumerate(reader()):
            self.assertEqual(e, str("0"))

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

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

            return reader

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

G
gongweibao 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        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 已提交
94

Y
Yi Wang 已提交
95

96 97
if __name__ == '__main__':
    unittest.main()