File: TestTimeConversion.cpp

package info (click to toggle)
soapysdr 0.8.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 948 kB
  • sloc: cpp: 5,378; ansic: 471; python: 311; sh: 21; makefile: 18
file content (90 lines) | stat: -rw-r--r-- 3,410 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2015-2015 Josh Blum
// SPDX-License-Identifier: BSL-1.0

#include <SoapySDR/Time.hpp>
#include <cstdlib>
#include <cstdio>
#include <cmath>

static bool loopbackTimeToTicks(const long long timeNs, const double rate)
{
    const long long ticks = SoapySDR::timeNsToTicks(timeNs, rate);
    const long long outTime = SoapySDR::ticksToTimeNs(ticks, rate);
    //we expect error because timeNs specifies a sub-tick
    if (std::abs(timeNs - outTime)/1e9 < rate) return true;
    printf("FAIL: loopbackTimeToTicks(%lld, %f)\n", timeNs, rate);
    printf("    ticks = %lld, outTime = %lld\n", ticks, outTime);
    printf("    error = %f secs\n", std::abs(timeNs - outTime)/1e9);
    return false;
}

static bool loopbackTicksToTime(const long long ticks, const double rate)
{
    const long long timeNs = SoapySDR::ticksToTimeNs(ticks, rate);
    const long long outTicks = SoapySDR::timeNsToTicks(timeNs, rate);
    if (std::abs(ticks - outTicks) == 0) return true;
    printf("FAIL: loopbackTicksToTime(%lld, %f)\n", ticks, rate);
    printf("    timeNs = %lld, outTicks = %lld\n", timeNs, outTicks);
    printf("    error = %d ticks\n", int(std::abs(ticks - outTicks)));
    return false;
}

//http://stackoverflow.com/questions/8120062/generate-random-64-bit-integer
static unsigned rand256(void)
{
    static unsigned const limit = RAND_MAX - RAND_MAX % 256;
    unsigned result = std::rand();
    while ( result >= limit )
    {
        result = std::rand();
    }
    return result % 256;
}

static unsigned long long rand64bits(void)
{
    unsigned long long results = 0ULL;
    for ( int count = 8; count > 0; -- count)
    {
        results = 256U * results + rand256();
    }
    return results;
}

int main(void)
{
    //test that random times can make it through the conversion
    printf("Test random times...\n");
    for (size_t i = 0; i < 100; i++)
    {
        const long long timeNs = rand64bits();
        if (not loopbackTimeToTicks(timeNs, 1e9)) return EXIT_FAILURE;
        if (not loopbackTimeToTicks(-timeNs, 1e9)) return EXIT_FAILURE;
        if (not loopbackTimeToTicks(timeNs, 52e6)) return EXIT_FAILURE;
        if (not loopbackTimeToTicks(-timeNs, 52e6)) return EXIT_FAILURE;
        if (not loopbackTimeToTicks(timeNs, 61.44e6)) return EXIT_FAILURE;
        if (not loopbackTimeToTicks(-timeNs, 61.44e6)) return EXIT_FAILURE;
        if (not loopbackTimeToTicks(timeNs, 100e6/3)) return EXIT_FAILURE;
        if (not loopbackTimeToTicks(-timeNs, 100e6/3)) return EXIT_FAILURE;
    }
    printf("OK\n");

    //test random ticks for several different rates
    printf("Test random ticks...\n");
    for (size_t i = 0; i < 100; i++)
    {
        const long long ticks = rand64bits() >> 8; //room for max rate
        if (not loopbackTicksToTime(ticks, 1e9)) return EXIT_FAILURE;
        if (not loopbackTicksToTime(-ticks, 1e9)) return EXIT_FAILURE;
        if (not loopbackTicksToTime(ticks, 52e6)) return EXIT_FAILURE;
        if (not loopbackTicksToTime(-ticks, 52e6)) return EXIT_FAILURE;
        if (not loopbackTicksToTime(ticks, 61.44e6)) return EXIT_FAILURE;
        if (not loopbackTicksToTime(-ticks, 61.44e6)) return EXIT_FAILURE;
        if (not loopbackTicksToTime(ticks, 100e6/3)) return EXIT_FAILURE;
        if (not loopbackTicksToTime(-ticks, 100e6/3)) return EXIT_FAILURE;
    }
    printf("OK\n");

    printf("DONE!\n");
    return EXIT_SUCCESS;
}