00001
00002
00003
00004 import os
00005 import logging
00006 import unittest
00007 import ConfigParser
00008
00009 PKG_NAME = 'rospeex_core'
00010 try:
00011 import roslib; roslib.load_manifest(PKG_NAME)
00012 except:
00013 pass
00014
00015 import rospeex_core.validators as validators
00016 from rospeex_core.exceptions import UnsupportedLanguageException
00017 from rospeex_core.exceptions import InvalidAudioDataException
00018
00019
00020 logger = logging.getLogger(__name__)
00021 logger.setLevel(logging.DEBUG)
00022 formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(filename)s:%(lineno)d:%(message)s')
00023 ch = logging.StreamHandler()
00024 ch.setFormatter(formatter)
00025 logger.addHandler(ch)
00026
00027
00028 class TestValidators(unittest.TestCase):
00029 """ rospeex.validators unittest class """
00030
00031 AUDIO_LENGTH = 8000
00032 SUPPORTED_LANGUAGES = ['ja', 'en', 'zhe', '*']
00033 FRAMERATE = 16000
00034 CHANNELS = 1
00035 SAMPWIDTH = 2
00036
00037 def setUp(self):
00038 """ setup class """
00039 base_dir = os.path.dirname(__file__)
00040 settings = ConfigParser.ConfigParser()
00041 filename = os.path.join(base_dir, 'config.cfg')
00042 settings.read(filename)
00043 self.flac_file = os.path.join(base_dir, settings.get('Validators', 'flac_file'))
00044 self.wav_file = os.path.join(base_dir, settings.get('Validators', 'wav_file'))
00045 self.big_wav_file = os.path.join(base_dir, settings.get('Validators', 'big_wav_file'))
00046 self.broken_wav_file = os.path.join(base_dir, settings.get('Validators', 'broken_wav_file'))
00047
00048 def test_supported_languages(self):
00049 failed = False
00050 err_msg = ''
00051
00052
00053 try:
00054 validators.check_language('hoge', self.SUPPORTED_LANGUAGES)
00055 failed = True
00056 except UnsupportedLanguageException as err:
00057 err_msg = str(err)
00058 logger.info(err_msg)
00059 self.failIf(failed, 'fail to validate')
00060
00061
00062 try:
00063 validators.check_language('ja', self.SUPPORTED_LANGUAGES)
00064 except UnsupportedLanguageException as err:
00065 err_msg = str(err)
00066 failed = True
00067 logger.info(err_msg)
00068 self.failIf(failed, 'fail to validate. Exception:%s' % err_msg)
00069
00070
00071 try:
00072 validators.check_language('*', self.SUPPORTED_LANGUAGES)
00073 except UnsupportedLanguageException as err:
00074 err_msg = str(err)
00075 failed = True
00076 logger.info(err_msg)
00077 self.failIf(failed, 'fail to validate. Exception:%s' % err_msg)
00078
00079
00080 def test_wave_file(self):
00081 failed = False
00082 err_msg = ''
00083
00084
00085 try:
00086 data = open(self.broken_wav_file, 'rb').read()
00087 validators.check_wave_data(data, self.FRAMERATE, self.CHANNELS, self.SAMPWIDTH, self.AUDIO_LENGTH)
00088 failed = True
00089 except InvalidAudioDataException as err:
00090 err_msg = str(err)
00091 logger.info(err_msg)
00092 self.failIf(failed, 'broken wave data')
00093
00094
00095 try:
00096 data = open(self.flac_file, 'rb').read()
00097 validators.check_wave_data(data, self.FRAMERATE, self.CHANNELS, self.SAMPWIDTH, self.AUDIO_LENGTH)
00098 failed = True
00099 except InvalidAudioDataException as err:
00100 err_msg = str(err)
00101 logger.info(err_msg)
00102 self.failIf(failed, 'non wave data')
00103
00104
00105 try:
00106 data = open(self.wav_file, 'rb').read()
00107 validators.check_wave_data(data, self.FRAMERATE, 2, self.SAMPWIDTH, self.AUDIO_LENGTH)
00108 failed = True
00109 except InvalidAudioDataException as err:
00110 err_msg = str(err)
00111 logger.info(err_msg)
00112 except Exception as err:
00113 failed = True
00114 err_msg = str(err)
00115 logger.info(err_msg)
00116 self.failIf(failed, 'invalid format nchannel %s' % err_msg)
00117
00118
00119 try:
00120 data = open(self.wav_file, 'rb').read()
00121 validators.check_wave_data(data, self.FRAMERATE+1000, self.CHANNELS, self.SAMPWIDTH, self.AUDIO_LENGTH)
00122 failed = True
00123 except InvalidAudioDataException as err:
00124 err_msg = str(err)
00125 logger.info(err_msg)
00126 except Exception as err:
00127 failed = True
00128 err_msg = str(err)
00129 logger.info(err_msg)
00130 self.failIf(failed, 'invalid format framerate %s' % err_msg)
00131
00132
00133 try:
00134 data = open(self.wav_file, 'rb').read()
00135 validators.check_wave_data(data, self.FRAMERATE, self.CHANNELS, self.SAMPWIDTH+2, self.AUDIO_LENGTH)
00136 failed = True
00137 except InvalidAudioDataException as err:
00138 err_msg = str(err)
00139 logger.info(err_msg)
00140 except Exception as err:
00141 failed = True
00142 err_msg = str(err)
00143 logger.info(err_msg)
00144 self.failIf(failed, 'invalid format sampwidth %s' % err_msg)
00145
00146
00147 try:
00148 data = open(self.big_wav_file, 'rb').read()
00149 validators.check_wave_data(data, self.FRAMERATE, self.CHANNELS, self.SAMPWIDTH, self.AUDIO_LENGTH)
00150 failed = True
00151 except InvalidAudioDataException as err:
00152 err_msg = str(err)
00153 logger.info(err_msg)
00154 except Exception as err:
00155 failed = True
00156 err_msg = str(err)
00157 logger.info(err_msg)
00158 self.failIf(failed, 'too big data size sampwidth %s' % err_msg)
00159
00160
00161 try:
00162 data = open(self.wav_file, 'rb').read()
00163 validators.check_wave_data(data, self.FRAMERATE, self.CHANNELS, self.SAMPWIDTH, self.AUDIO_LENGTH)
00164 except InvalidAudioDataException as err:
00165 failed = True
00166 err_msg = str(err)
00167 logger.info(err_msg)
00168 self.failIf(failed, 'invalid validate %s' % err_msg)
00169
00170
00171
00172 if __name__ == '__main__':
00173 import rosunit
00174 rosunit.unitrun(PKG_NAME, 'validator', TestValidators, None, coverage_packages=['rospeex.validators'])
00175