Refactor freqman_db parsing (#1244)

* WIP freqman changes/memory perf/stash
* Split ui tone_key function out for testing.
* Add more tests and fix bugs.
* Use default max_entries in recond
* Set limit back to 90 for now
This commit is contained in:
Kyle Reed 2023-07-08 13:04:12 -07:00 committed by GitHub
parent 60de625c37
commit 497ca3f934
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 1206 additions and 705 deletions

View file

@ -39,11 +39,19 @@ add_executable(application_test EXCLUDE_FROM_ALL
${PROJECT_SOURCE_DIR}/test_convert.cpp
${PROJECT_SOURCE_DIR}/test_file_reader.cpp
${PROJECT_SOURCE_DIR}/test_file_wrapper.cpp
${PROJECT_SOURCE_DIR}/test_freqman_db.cpp
${PROJECT_SOURCE_DIR}/test_mock_file.cpp
${PROJECT_SOURCE_DIR}/test_optional.cpp
${PROJECT_SOURCE_DIR}/test_utility.cpp
${PROJECT_SOURCE_DIR}/../../application/file_reader.cpp
${PROJECT_SOURCE_DIR}/../../application/freqman_db.cpp
${PROJECT_SOURCE_DIR}/../../application/string_format.cpp
# Dependencies
${PROJECT_SOURCE_DIR}/../../application/file.cpp
${PROJECT_SOURCE_DIR}/../../application/tone_key.cpp
${PROJECT_SOURCE_DIR}/linker_stubs.cpp
)
target_include_directories(application_test PRIVATE

View file

@ -0,0 +1,80 @@
/*
* Copyright (C) 2023 Kyle Reed
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
/* This file contains stub functions necessary to enable linking.
* Try to minimize dependecies by breaking code into separate files
* or using templates and mock types. Because the test code is built
* and executed on the dev machine, a lot of core firmware code
* will not or cannot work (e.g. filesystem). We could build abstractions
* but that's just device overhead that only supports testing. */
#include <string>
/* FatFS stubs */
#include "ff.h"
FRESULT f_close(FIL*) {
return FR_OK;
}
FRESULT f_closedir(DIR*) {
return FR_OK;
}
FRESULT f_findfirst(DIR*, FILINFO*, const TCHAR*, const TCHAR*) {
return FR_OK;
}
FRESULT f_findnext(DIR*, FILINFO*) {
return FR_OK;
}
FRESULT f_getfree(const TCHAR*, DWORD*, FATFS**) {
return FR_OK;
}
FRESULT f_lseek(FIL*, FSIZE_t) {
return FR_OK;
}
FRESULT f_mkdir(const TCHAR*) {
return FR_OK;
}
FRESULT f_open(FIL*, const TCHAR*, BYTE) {
return FR_OK;
}
FRESULT f_read(FIL*, void*, UINT, UINT*) {
return FR_OK;
}
FRESULT f_rename(const TCHAR*, const TCHAR*) {
return FR_OK;
}
FRESULT f_stat(const TCHAR*, FILINFO*) {
return FR_OK;
}
FRESULT f_sync(FIL*) {
return FR_OK;
}
FRESULT f_truncate(FIL*) {
return FR_OK;
}
FRESULT f_unlink(const TCHAR*) {
return FR_OK;
}
FRESULT f_write(FIL*, const void*, UINT, UINT*) {
return FR_OK;
}
/* Debug */
void __debug_log(const std::string&) {}

View file

@ -38,6 +38,20 @@ TEST_CASE("It can iterate file lines.") {
CHECK_EQ(line_count, 3);
}
TEST_CASE("It can iterate multiple times.") {
MockFile f{"abc\ndef\nhij"};
BufferLineReader<MockFile> reader{f};
int line_count = 0;
int line_count2 = 0;
for (const auto& line : reader)
++line_count;
for (const auto& line : reader)
++line_count2;
CHECK_EQ(line_count, 3);
CHECK_EQ(line_count2, 3);
}
TEST_CASE("It can iterate file ending with newline.") {
MockFile f{"abc\ndef\nhij\n"};
BufferLineReader<MockFile> reader{f};
@ -133,6 +147,18 @@ TEST_CASE("It will split only empty columns.") {
TEST_SUITE_END();
TEST_CASE("count_lines returns 1 for single line") {
MockFile f{"abs"};
BufferLineReader<MockFile> reader{f};
CHECK_EQ(count_lines(reader), 1);
}
TEST_CASE("count_lines returns 2 for 2 lines") {
MockFile f{"abs"};
BufferLineReader<MockFile> reader{f};
CHECK_EQ(count_lines(reader), 1);
}
/* Simple example of how to use this to read settings by lines. */
TEST_CASE("It can parse a settings file.") {
MockFile f{"100,File.txt,5\n200,File2.txt,7"};

View file

@ -0,0 +1,217 @@
/*
* Copyright (C) 2023 Kyle Reed
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "doctest.h"
#include "freqman_db.hpp"
TEST_SUITE_BEGIN("Freqman Parsing");
TEST_CASE("It can parse basic single freq entry.") {
freqman_entry e;
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.", e));
CHECK_EQ(e.frequency_a, 123'000'000);
CHECK_EQ(e.description, "This is the description.");
CHECK_EQ(e.type, freqman_type::Single);
}
TEST_CASE("It can parse basic range freq entry.") {
freqman_entry e;
REQUIRE(
parse_freqman_entry(
"a=123000000,b=423000000,d=This is the description.", e));
CHECK_EQ(e.frequency_a, 123'000'000);
CHECK_EQ(e.frequency_b, 423'000'000);
CHECK_EQ(e.description, "This is the description.");
CHECK_EQ(e.type, freqman_type::Range);
}
TEST_CASE("It can parse basic ham radio freq entry.") {
freqman_entry e;
REQUIRE(
parse_freqman_entry(
"r=123000000,t=423000000,d=This is the description.", e));
CHECK_EQ(e.frequency_a, 123'000'000);
CHECK_EQ(e.frequency_b, 423'000'000);
CHECK_EQ(e.description, "This is the description.");
CHECK_EQ(e.type, freqman_type::HamRadio);
}
TEST_CASE("It can parse modulation") {
freqman_entry e;
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,m=AM", e));
CHECK_EQ(e.modulation, 0);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,m=NFM", e));
CHECK_EQ(e.modulation, 1);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,m=WFM", e));
CHECK_EQ(e.modulation, 2);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,m=SPEC", e));
CHECK_EQ(e.modulation, 3);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,m=FOO", e));
CHECK_EQ(e.modulation, freqman_invalid_index);
}
TEST_CASE("It can parse bandwidth") {
freqman_entry e;
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,m=AM,bw=DSB 6k", e));
CHECK_EQ(e.bandwidth, 1);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,bw=DSB 6k", e));
// Modulation wasn't set.
CHECK_EQ(e.bandwidth, freqman_invalid_index);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,m=WFM,bw=50k", e));
// Invalid bandwidth value.
CHECK_EQ(e.bandwidth, freqman_invalid_index);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,m=SPEC,bw=16k", e));
CHECK_EQ(e.modulation, 3);
REQUIRE(
parse_freqman_entry(
"f=123000000,m=NFM,bw=11k,d=This is the description.", e));
CHECK_EQ(e.modulation, 1);
}
TEST_CASE("It can parse frequency step") {
freqman_entry e;
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,s=0.1kHz", e));
CHECK_EQ(e.step, 0);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,s=50kHz", e));
CHECK_EQ(e.step, 11);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,s=FOO", e));
CHECK_EQ(e.step, freqman_invalid_index);
}
TEST_CASE("It can parse tone freq") {
freqman_entry e;
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,c=0.0", e));
CHECK_EQ(e.tone, 0);
REQUIRE(
parse_freqman_entry(
"f=123000000,c=67.0,d=This is the description.", e));
CHECK_EQ(e.tone, 1);
REQUIRE(
parse_freqman_entry(
"f=123000000,c=67,d=This is the description.", e));
// Fractional can be omitted.
CHECK_EQ(e.tone, 1);
REQUIRE(
parse_freqman_entry(
"f=123000000,c=69.33,d=This is the description.", e));
// Fractional extra digits can be omitted.
CHECK_EQ(e.tone, 2);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,c=72", e));
// Should choose nearest.
CHECK_EQ(e.tone, 3);
}
#if 0 // New tables for a future PR.
TEST_CASE("It can parse modulation") {
freqman_entry e;
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,m=AM", e));
CHECK_EQ(e.modulation, freqman_modulation::AM);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,m=NFM", e));
CHECK_EQ(e.modulation, freqman_modulation::NFM);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,m=WFM", e));
CHECK_EQ(e.modulation, freqman_modulation::WFM);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,m=SPEC", e));
CHECK_EQ(e.modulation, freqman_modulation::SPEC);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,m=FOO", e));
CHECK_EQ(e.modulation, freqman_modulation::Unknown);
}
TEST_CASE("It can parse frequency step") {
freqman_entry e;
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,s=0.1kHz", e));
CHECK_EQ(e.step, freqman_step::_100Hz);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,s=50kHz", e));
CHECK_EQ(e.step, freqman_step::_50kHz);
REQUIRE(
parse_freqman_entry(
"f=123000000,d=This is the description.,s=FOO", e));
CHECK_EQ(e.step, freqman_step::Unknown);
}
#endif
TEST_SUITE_END();