blob: 030d373c9b26ec1c4966d582ff616dd3898b2e3b [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2014. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include <Preferences.h>
9#include <Timer.h>
10#include "gtest/gtest.h"
11#include <cstdio>
12#include <fstream>
13
14static const char *kFileName = "networktables.ini";
15static const double kSaveTime = 1.2;
16
17/**
18 * If we write a new networktables.ini with some sample values, test that
19 * we get those same values back using the Preference class.
20 */
21TEST(PreferencesTest, ReadPreferencesFromFile) {
22 NetworkTable::Shutdown();
23 std::remove(kFileName);
24 std::ofstream preferencesFile(kFileName);
25 preferencesFile << "[NetworkTables Storage 3.0]" << std::endl;
26 preferencesFile << "string \"/Preferences/testFileGetString\"=\"Hello, preferences file\""
27 << std::endl;
28 preferencesFile << "double \"/Preferences/testFileGetInt\"=1" << std::endl;
29 preferencesFile << "double \"/Preferences/testFileGetDouble\"=0.5" << std::endl;
30 preferencesFile << "double \"/Preferences/testFileGetFloat\"=0.25" << std::endl;
31 preferencesFile << "boolean \"/Preferences/testFileGetBoolean\"=true" << std::endl;
32 preferencesFile << "double \"/Preferences/testFileGetLong\"=1000000000000000000" << std::endl;
33 preferencesFile.close();
34 NetworkTable::Initialize();
35
36 Preferences *preferences = Preferences::GetInstance();
37 EXPECT_EQ("Hello, preferences file",
38 preferences->GetString("testFileGetString"));
39 EXPECT_EQ(1, preferences->GetInt("testFileGetInt"));
40 EXPECT_FLOAT_EQ(0.5, preferences->GetDouble("testFileGetDouble"));
41 EXPECT_FLOAT_EQ(0.25f, preferences->GetFloat("testFileGetFloat"));
42 EXPECT_TRUE(preferences->GetBoolean("testFileGetBoolean"));
43 EXPECT_EQ(1000000000000000000ll, preferences->GetLong("testFileGetLong"));
44}
45
46/**
47 * If we set some values using the Preferences class, test that they show up
48 * in networktables.ini
49 */
50TEST(PreferencesTest, WritePreferencesToFile) {
51 NetworkTable::Shutdown();
52 NetworkTable::GlobalDeleteAll();
53 std::remove(kFileName);
54 NetworkTable::Initialize();
55 Preferences *preferences = Preferences::GetInstance();
56 preferences->PutString("testFilePutString", "Hello, preferences file");
57 preferences->PutInt("testFilePutInt", 1);
58 preferences->PutDouble("testFilePutDouble", 0.5);
59 preferences->PutFloat("testFilePutFloat", 0.25f);
60 preferences->PutBoolean("testFilePutBoolean", true);
61 preferences->PutLong("testFilePutLong", 1000000000000000000ll);
62 preferences->Save();
63
64 Wait(kSaveTime);
65
66 static char const *kExpectedFileContents[] = {
67 "[NetworkTables Storage 3.0]",
68 "boolean \"/Preferences/testFilePutBoolean\"=true",
69 "double \"/Preferences/testFilePutDouble\"=0.5",
70 "double \"/Preferences/testFilePutFloat\"=0.25",
71 "double \"/Preferences/testFilePutInt\"=1",
72 "double \"/Preferences/testFilePutLong\"=1e+18",
73 "string \"/Preferences/testFilePutString\"=\"Hello, preferences file\""};
74
75 std::ifstream preferencesFile(kFileName);
76 for (auto& kExpectedFileContent : kExpectedFileContents) {
77 ASSERT_FALSE(preferencesFile.eof())
78 << "Preferences file prematurely reached EOF";
79
80 std::string line;
81 std::getline(preferencesFile, line);
82
83 ASSERT_EQ(kExpectedFileContent, line)
84 << "A line in networktables.ini was not correct";
85 }
86}