|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright (C) 2016 Swift Navigation Inc. |
| 3 | +# |
| 4 | +# Contact: Pasi Miettinen <[email protected]> |
| 5 | +# This source is subject to the license found in the file 'LICENSE' which must |
| 6 | +# be be distributed together with this source. All other rights reserved. |
| 7 | +# |
| 8 | +# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, |
| 9 | +# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED |
| 10 | +# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | + |
| 12 | +''' |
| 13 | +Unit tests for IQgen factories |
| 14 | +''' |
| 15 | + |
| 16 | +from peregrine.iqgen.bits.amplitude_factory import factoryObject as AFO |
| 17 | +from peregrine.iqgen.bits.doppler_factory import factoryObject as DFO |
| 18 | +from peregrine.iqgen.bits.message_factory import factoryObject as MFO |
| 19 | +from peregrine.iqgen.bits.satellite_factory import factoryObject as SFO |
| 20 | +from peregrine.iqgen.bits.tcxo_factory import factoryObject as TFO |
| 21 | + |
| 22 | +from peregrine.iqgen.bits.amplitude_base import AmplitudeBase |
| 23 | +from peregrine.iqgen.bits.amplitude_poly import AmplitudePoly |
| 24 | +from peregrine.iqgen.bits.amplitude_sine import AmplitudeSine |
| 25 | + |
| 26 | +from peregrine.iqgen.bits.doppler_poly import Doppler as DopplerPoly |
| 27 | +from peregrine.iqgen.bits.doppler_sine import Doppler as DopplerSine |
| 28 | + |
| 29 | +from peregrine.iqgen.bits.message_block import Message as BlockMessage |
| 30 | +from peregrine.iqgen.bits.message_cnav import Message as CNAVMessage |
| 31 | +from peregrine.iqgen.bits.message_const import Message as ConstMessage |
| 32 | +from peregrine.iqgen.bits.message_lnav import Message as LNAVMessage |
| 33 | +from peregrine.iqgen.bits.message_zeroone import Message as ZeroOneMessage |
| 34 | +from peregrine.iqgen.bits.message_glo import Message as GLOMessage |
| 35 | + |
| 36 | +from peregrine.iqgen.bits.satellite_gps import GPSSatellite |
| 37 | +from peregrine.iqgen.bits.satellite_glo import GLOSatellite |
| 38 | + |
| 39 | +from peregrine.iqgen.bits.tcxo_poly import TCXOPoly as PolyTcxo |
| 40 | +from peregrine.iqgen.bits.tcxo_sine import TCXOSine as SineTcxo |
| 41 | + |
| 42 | +from peregrine.iqgen.iqgen_main import prepareArgsParser |
| 43 | + |
| 44 | +import numpy as np |
| 45 | +import os |
| 46 | + |
| 47 | + |
| 48 | +class Dummy(object): |
| 49 | + pass |
| 50 | + |
| 51 | + |
| 52 | +def to_map_and_back(factory, instance): |
| 53 | + mapped = factory.fromMapForm(factory.toMapForm(instance)) |
| 54 | + |
| 55 | + assert isinstance(mapped, instance.__class__) |
| 56 | + for key, value in mapped.__dict__.iteritems(): |
| 57 | + if isinstance(mapped.__dict__[key], np.ndarray): |
| 58 | + assert (mapped.__dict__[key] == mapped.__dict__[key]).all() |
| 59 | + else: |
| 60 | + assert mapped.__dict__[key] == mapped.__dict__[key] |
| 61 | + |
| 62 | + |
| 63 | +def value_error(factory): |
| 64 | + try: |
| 65 | + factory.toMapForm(Dummy()) |
| 66 | + assert False |
| 67 | + except ValueError: |
| 68 | + pass |
| 69 | + |
| 70 | + try: |
| 71 | + factory.fromMapForm({'type': 'Dummy'}) |
| 72 | + assert False |
| 73 | + except ValueError: |
| 74 | + pass |
| 75 | + |
| 76 | + |
| 77 | +def test_factories(): |
| 78 | + ''' |
| 79 | + Test factories |
| 80 | + ''' |
| 81 | + to_map_and_back(AFO, AmplitudePoly(AmplitudeBase.UNITS_AMPLITUDE, (1, ))) |
| 82 | + to_map_and_back(AFO, AmplitudeSine(AmplitudeBase.UNITS_AMPLITUDE, 1., 2., 1.)) |
| 83 | + value_error(AFO) |
| 84 | + |
| 85 | + to_map_and_back(DFO, DopplerPoly(1000., 77., (1., 1.))) |
| 86 | + to_map_and_back(DFO, DopplerSine(1000., 55., 4., 3., 5.)) |
| 87 | + value_error(DFO) |
| 88 | + |
| 89 | + to_map_and_back(MFO, BlockMessage(np.random.rand(1023))) |
| 90 | + to_map_and_back(MFO, CNAVMessage(1)) |
| 91 | + to_map_and_back(MFO, ConstMessage(1)) |
| 92 | + to_map_and_back(MFO, LNAVMessage(1)) |
| 93 | + to_map_and_back(MFO, ZeroOneMessage()) |
| 94 | + to_map_and_back(MFO, GLOMessage(1)) |
| 95 | + value_error(MFO) |
| 96 | + |
| 97 | + to_map_and_back(SFO, GPSSatellite(1)) |
| 98 | + to_map_and_back(SFO, GLOSatellite(1)) |
| 99 | + value_error(SFO) |
| 100 | + |
| 101 | + to_map_and_back(TFO, PolyTcxo((1., 1.))) |
| 102 | + to_map_and_back(TFO, SineTcxo(0., 1e6, 0.004)) |
| 103 | + value_error(TFO) |
| 104 | + |
| 105 | + |
| 106 | +def test_config(): |
| 107 | + ''' |
| 108 | + Test config |
| 109 | + ''' |
| 110 | + try: |
| 111 | + CONFIG_FILE = 'test.conf' |
| 112 | + parser = prepareArgsParser() |
| 113 | + saved = parser.parse_args('--save-config {0}'.format(CONFIG_FILE).split()) |
| 114 | + loaded = parser.parse_args('--load-config {0}'.format(CONFIG_FILE).split()) |
| 115 | + |
| 116 | + finally: |
| 117 | + if os.path.isfile(CONFIG_FILE): |
| 118 | + os.remove(CONFIG_FILE) |
| 119 | + |
| 120 | + assert saved.__dict__['tcxo'].__dict__ == loaded.__dict__['tcxo'].__dict__ |
| 121 | + |
| 122 | + for key, value in saved.__dict__.iteritems(): |
| 123 | + if key == 'no_run' or key == 'tcxo': |
| 124 | + continue |
| 125 | + assert saved.__dict__[key] == loaded.__dict__[key] |
0 commit comments