|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2020 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""Tests for combining training corpora.""" |
| 16 | + |
| 17 | +import json |
| 18 | +import os |
| 19 | + |
| 20 | +from absl.testing import absltest |
| 21 | + |
| 22 | +from compiler_opt.tools import combine_training_corpus_lib |
| 23 | + |
| 24 | + |
| 25 | +class CombineTrainingCorpusTest(absltest.TestCase): |
| 26 | + |
| 27 | + def test_combine_corpus(self): |
| 28 | + corpus_dir = self.create_tempdir() |
| 29 | + subcorpus1_dir = corpus_dir.mkdir(dir_path='subcorpus1') |
| 30 | + subcorpus2_dir = corpus_dir.mkdir(dir_path='subcorpus2') |
| 31 | + subcorpus1_description = { |
| 32 | + 'has_thinlto': False, |
| 33 | + 'modules': ['test1.o', 'test2.o'] |
| 34 | + } |
| 35 | + subcorpus2_description = { |
| 36 | + 'has_thinlto': False, |
| 37 | + 'modules': ['test3.o', 'test4.o'] |
| 38 | + } |
| 39 | + subcorpus1_description_file = subcorpus1_dir.create_file( |
| 40 | + file_path='corpus_description.json') |
| 41 | + subcorpus2_description_file = subcorpus2_dir.create_file( |
| 42 | + file_path='corpus_description.json') |
| 43 | + subcorpus1_description_file.write_text(json.dumps(subcorpus1_description)) |
| 44 | + subcorpus2_description_file.write_text(json.dumps(subcorpus2_description)) |
| 45 | + combine_training_corpus_lib.combine_corpus(corpus_dir.full_path) |
| 46 | + with open( |
| 47 | + os.path.join(corpus_dir, 'corpus_description.json'), |
| 48 | + encoding='utf-8') as combined_corpus_description_file: |
| 49 | + combined_corpus_description = json.load(combined_corpus_description_file) |
| 50 | + self.assertEqual(combined_corpus_description['has_thinlto'], False) |
| 51 | + self.assertLen(combined_corpus_description['modules'], 4) |
| 52 | + self.assertIn('subcorpus1/test1.o', combined_corpus_description['modules']) |
| 53 | + self.assertIn('subcorpus1/test2.o', combined_corpus_description['modules']) |
| 54 | + self.assertIn('subcorpus2/test3.o', combined_corpus_description['modules']) |
| 55 | + self.assertIn('subcorpus2/test4.o', combined_corpus_description['modules']) |
| 56 | + |
| 57 | + def test_different_corpora(self): |
| 58 | + corpus_dir = self.create_tempdir() |
| 59 | + subcorpus1_dir = corpus_dir.mkdir(dir_path='subcorpus1') |
| 60 | + subcorpus2_dir = corpus_dir.mkdir(dir_path='subcorpus2') |
| 61 | + subcorpus1_description = {'has_thinlto': False, 'modules': ['test1.o']} |
| 62 | + subcorpus2_description = {'has_thinlto': True, 'modules': ['test2.o']} |
| 63 | + subcorpus1_description_file = subcorpus1_dir.create_file( |
| 64 | + file_path='corpus_description.json') |
| 65 | + subcorpus2_description_file = subcorpus2_dir.create_file( |
| 66 | + file_path='corpus_description.json') |
| 67 | + subcorpus1_description_file.write_text(json.dumps(subcorpus1_description)) |
| 68 | + subcorpus2_description_file.write_text(json.dumps(subcorpus2_description)) |
| 69 | + self.assertRaises(ValueError, combine_training_corpus_lib.combine_corpus, |
| 70 | + corpus_dir.full_path) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + absltest.main() |
0 commit comments