2014-07-30 17:32:14 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-23 22:06:13 -07:00
|
|
|
/* Copyright (c) 2014-2017 FIRST. All Rights Reserved. */
|
2014-07-30 17:32:14 -04:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2016-09-05 13:55:31 -07:00
|
|
|
#include "Preferences.h" // NOLINT(build/include_order)
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2016-09-14 20:52:06 -07:00
|
|
|
#include <cstdio>
|
2016-09-05 13:55:31 -07:00
|
|
|
#include <fstream>
|
2014-07-30 17:32:14 -04:00
|
|
|
|
2017-12-07 23:34:29 -08:00
|
|
|
#include <networktables/NetworkTableInstance.h>
|
|
|
|
|
|
2016-05-25 22:38:11 -07:00
|
|
|
#include "Timer.h"
|
2016-09-05 13:55:31 -07:00
|
|
|
#include "gtest/gtest.h"
|
2016-06-20 22:17:40 -07:00
|
|
|
#include "ntcore.h"
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
static const char* kFileName = "networktables.ini";
|
2015-08-13 23:17:19 -07:00
|
|
|
static const double kSaveTime = 1.2;
|
2014-07-30 17:32:14 -04:00
|
|
|
|
|
|
|
|
/**
|
2015-08-13 23:17:19 -07:00
|
|
|
* If we write a new networktables.ini with some sample values, test that
|
2014-07-30 17:32:14 -04:00
|
|
|
* we get those same values back using the Preference class.
|
|
|
|
|
*/
|
|
|
|
|
TEST(PreferencesTest, ReadPreferencesFromFile) {
|
2017-09-07 22:40:18 -07:00
|
|
|
auto inst = nt::NetworkTableInstance::GetDefault();
|
|
|
|
|
inst.StopServer();
|
2015-06-25 15:07:55 -04:00
|
|
|
std::remove(kFileName);
|
|
|
|
|
std::ofstream preferencesFile(kFileName);
|
2015-08-13 23:17:19 -07:00
|
|
|
preferencesFile << "[NetworkTables Storage 3.0]" << std::endl;
|
2016-05-20 17:30:37 -07:00
|
|
|
preferencesFile
|
|
|
|
|
<< "string \"/Preferences/testFileGetString\"=\"Hello, preferences file\""
|
|
|
|
|
<< std::endl;
|
2015-08-13 23:17:19 -07:00
|
|
|
preferencesFile << "double \"/Preferences/testFileGetInt\"=1" << std::endl;
|
2016-05-20 17:30:37 -07:00
|
|
|
preferencesFile << "double \"/Preferences/testFileGetDouble\"=0.5"
|
|
|
|
|
<< std::endl;
|
|
|
|
|
preferencesFile << "double \"/Preferences/testFileGetFloat\"=0.25"
|
|
|
|
|
<< std::endl;
|
|
|
|
|
preferencesFile << "boolean \"/Preferences/testFileGetBoolean\"=true"
|
|
|
|
|
<< std::endl;
|
|
|
|
|
preferencesFile
|
|
|
|
|
<< "double \"/Preferences/testFileGetLong\"=1000000000000000000"
|
|
|
|
|
<< std::endl;
|
2015-06-25 15:07:55 -04:00
|
|
|
preferencesFile.close();
|
2017-09-07 22:40:18 -07:00
|
|
|
inst.StartServer();
|
2014-07-30 17:32:14 -04:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
Preferences* preferences = Preferences::GetInstance();
|
2015-06-25 15:07:55 -04:00
|
|
|
EXPECT_EQ("Hello, preferences file",
|
|
|
|
|
preferences->GetString("testFileGetString"));
|
|
|
|
|
EXPECT_EQ(1, preferences->GetInt("testFileGetInt"));
|
|
|
|
|
EXPECT_FLOAT_EQ(0.5, preferences->GetDouble("testFileGetDouble"));
|
|
|
|
|
EXPECT_FLOAT_EQ(0.25f, preferences->GetFloat("testFileGetFloat"));
|
|
|
|
|
EXPECT_TRUE(preferences->GetBoolean("testFileGetBoolean"));
|
|
|
|
|
EXPECT_EQ(1000000000000000000ll, preferences->GetLong("testFileGetLong"));
|
2014-07-30 17:32:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If we set some values using the Preferences class, test that they show up
|
2015-08-13 23:17:19 -07:00
|
|
|
* in networktables.ini
|
2014-07-30 17:32:14 -04:00
|
|
|
*/
|
|
|
|
|
TEST(PreferencesTest, WritePreferencesToFile) {
|
2017-09-07 22:40:18 -07:00
|
|
|
auto inst = nt::NetworkTableInstance::GetDefault();
|
|
|
|
|
inst.StartServer();
|
2016-05-20 17:30:37 -07:00
|
|
|
Preferences* preferences = Preferences::GetInstance();
|
2017-10-29 21:30:50 -07:00
|
|
|
preferences->Remove("testFileGetString");
|
|
|
|
|
preferences->Remove("testFileGetInt");
|
|
|
|
|
preferences->Remove("testFileGetDouble");
|
|
|
|
|
preferences->Remove("testFileGetFloat");
|
|
|
|
|
preferences->Remove("testFileGetBoolean");
|
|
|
|
|
preferences->Remove("testFileGetLong");
|
|
|
|
|
|
|
|
|
|
Wait(kSaveTime);
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
preferences->PutString("testFilePutString", "Hello, preferences file");
|
|
|
|
|
preferences->PutInt("testFilePutInt", 1);
|
|
|
|
|
preferences->PutDouble("testFilePutDouble", 0.5);
|
|
|
|
|
preferences->PutFloat("testFilePutFloat", 0.25f);
|
|
|
|
|
preferences->PutBoolean("testFilePutBoolean", true);
|
|
|
|
|
preferences->PutLong("testFilePutLong", 1000000000000000000ll);
|
2014-07-30 17:32:14 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Wait(kSaveTime);
|
2014-07-30 17:32:14 -04:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
static char const* kExpectedFileContents[] = {
|
2015-08-13 23:17:19 -07:00
|
|
|
"[NetworkTables Storage 3.0]",
|
2017-11-15 23:56:17 -08:00
|
|
|
"string \"/Preferences/.type\"=\"RobotPreferences\"",
|
2015-08-13 23:17:19 -07:00
|
|
|
"boolean \"/Preferences/testFilePutBoolean\"=true",
|
|
|
|
|
"double \"/Preferences/testFilePutDouble\"=0.5",
|
|
|
|
|
"double \"/Preferences/testFilePutFloat\"=0.25",
|
|
|
|
|
"double \"/Preferences/testFilePutInt\"=1",
|
|
|
|
|
"double \"/Preferences/testFilePutLong\"=1e+18",
|
|
|
|
|
"string \"/Preferences/testFilePutString\"=\"Hello, preferences file\""};
|
2014-07-30 17:32:14 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
std::ifstream preferencesFile(kFileName);
|
2015-06-23 04:49:51 -07:00
|
|
|
for (auto& kExpectedFileContent : kExpectedFileContents) {
|
2015-06-25 15:07:55 -04:00
|
|
|
ASSERT_FALSE(preferencesFile.eof())
|
|
|
|
|
<< "Preferences file prematurely reached EOF";
|
2014-07-30 17:32:14 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
std::string line;
|
|
|
|
|
std::getline(preferencesFile, line);
|
2014-07-30 17:32:14 -04:00
|
|
|
|
2015-06-23 04:49:51 -07:00
|
|
|
ASSERT_EQ(kExpectedFileContent, line)
|
2015-08-13 23:17:19 -07:00
|
|
|
<< "A line in networktables.ini was not correct";
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2014-07-30 17:32:14 -04:00
|
|
|
}
|