Files
allwpilib/wpilibcIntegrationTests/src/PreferencesTest.cpp
Fredric Silberberg 6d854afb0e WPILib Reorganization
This is a major restructuring of the WPILib repository to simply build
procedures and remove the remnants of Maven from everything except the
eclipse plugins. Gradle files have been largely simplified or rewritten,
taking advantage of splitting up parts of the build into separate build
files for ease of reading.

The eclipse plugins are now in a separate project, as is ntcore. All
dependencies are resolved via Maven dependencies, with the
Jenkins-maintained WPILib repo. Project structures have also been
simplified: we no longer have separate subprojects inside wpilibc and
wpilibj. Where possible, these changes hav been done with git renames,
to make sure we still have full history for all repositories. Other
unrelated subprojects have also been broken out: OutlineViewer is now a
separate project.

Change-Id: Ib4e2a6e1a2f66427a14f16612b0e0d69ed661878
2015-11-21 18:26:49 -05:00

87 lines
3.6 KiB
C++

/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2014. All Rights Reserved. */
/* 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. */
/*----------------------------------------------------------------------------*/
#include <Preferences.h>
#include <Timer.h>
#include "gtest/gtest.h"
#include <cstdio>
#include <fstream>
static const char *kFileName = "networktables.ini";
static const double kSaveTime = 1.2;
/**
* If we write a new networktables.ini with some sample values, test that
* we get those same values back using the Preference class.
*/
TEST(PreferencesTest, ReadPreferencesFromFile) {
NetworkTable::Shutdown();
std::remove(kFileName);
std::ofstream preferencesFile(kFileName);
preferencesFile << "[NetworkTables Storage 3.0]" << std::endl;
preferencesFile << "string \"/Preferences/testFileGetString\"=\"Hello, preferences file\""
<< std::endl;
preferencesFile << "double \"/Preferences/testFileGetInt\"=1" << std::endl;
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;
preferencesFile.close();
NetworkTable::Initialize();
Preferences *preferences = Preferences::GetInstance();
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"));
}
/**
* If we set some values using the Preferences class, test that they show up
* in networktables.ini
*/
TEST(PreferencesTest, WritePreferencesToFile) {
NetworkTable::Shutdown();
NetworkTable::GlobalDeleteAll();
std::remove(kFileName);
NetworkTable::Initialize();
Preferences *preferences = Preferences::GetInstance();
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();
Wait(kSaveTime);
static char const *kExpectedFileContents[] = {
"[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\""};
std::ifstream preferencesFile(kFileName);
for (auto& kExpectedFileContent : kExpectedFileContents) {
ASSERT_FALSE(preferencesFile.eof())
<< "Preferences file prematurely reached EOF";
std::string line;
std::getline(preferencesFile, line);
ASSERT_EQ(kExpectedFileContent, line)
<< "A line in networktables.ini was not correct";
}
}