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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
Replaced WPILib.h includes in integration tests with the minimum required subheaders to improve compilation times
I ran the benchmark in a tmpfs with an Intel Core i5-2430M. I ran it three times for each combination of build invokation and source tree.
First, I tested "make". For master (eb7d55f), I measured an average of 42.751s with a standard deviation of 0.372s. For this commit, I measured an average of 33.394s with a standard deviation of 0.140s. There was a 9.356s, or 22%, improvement with a total error of 1.3%.
Second, I tested "make -j4". For master (eb7d55f), I measured an average of 21.723s with a standard deviation of 0.158s. For this commit, I measured an average of 16.823s with a standard deviation of 0.340s. There was a 4.900s, or 23%, improvement with a total error of 2.7%.
Change-Id: Idb3adce62ed8ef449360c6583896b6da3565cf58
2015-07-22 02:34:12 -07:00
|
|
|
#include <Preferences.h>
|
|
|
|
|
#include <Timer.h>
|
2014-07-30 17:32:14 -04:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <fstream>
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "gtest/gtest.h"
|
2014-07-30 17:32:14 -04: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();
|
|
|
|
|
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
|
|
|
}
|