2014-07-30 17:32:14 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Copyright (c) FIRST 2014-2016. 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-05-25 22:38:11 -07:00
|
|
|
#include "Preferences.h"
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2014-07-30 17:32:14 -04:00
|
|
|
#include <fstream>
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "gtest/gtest.h"
|
2014-07-30 17:32:14 -04:00
|
|
|
|
2016-05-25 22:38:11 -07:00
|
|
|
#include "Timer.h"
|
2016-06-20 22:17:40 -07:00
|
|
|
#include "ntcore.h"
|
2016-05-25 22:38:11 -07:00
|
|
|
|
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) {
|
2015-08-13 23:17:19 -07:00
|
|
|
NetworkTable::Shutdown();
|
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();
|
2015-08-13 23:17:19 -07:00
|
|
|
NetworkTable::Initialize();
|
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) {
|
2015-08-13 23:17:19 -07:00
|
|
|
NetworkTable::Shutdown();
|
|
|
|
|
NetworkTable::GlobalDeleteAll();
|
2016-06-18 00:58:22 -07:00
|
|
|
// persistent keys don't get deleted normally, so make remaining keys
|
|
|
|
|
// non-persistent and delete them too
|
|
|
|
|
for (const auto& info : nt::GetEntryInfo("", 0)) {
|
|
|
|
|
nt::SetEntryFlags(info.name, 0);
|
|
|
|
|
}
|
|
|
|
|
NetworkTable::GlobalDeleteAll();
|
2015-08-13 23:17:19 -07:00
|
|
|
std::remove(kFileName);
|
|
|
|
|
NetworkTable::Initialize();
|
2016-05-20 17:30:37 -07:00
|
|
|
Preferences* preferences = Preferences::GetInstance();
|
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);
|
|
|
|
|
preferences->Save();
|
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]",
|
|
|
|
|
"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
|
|
|
}
|