blob: 60262fbd06e873b917ce6ca391e24b79349c4bd9 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2014-2017. 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" // NOLINT(build/include_order)
9
10#include <cstdio>
11#include <fstream>
12
13#include "Timer.h"
14#include "gtest/gtest.h"
15#include "ntcore.h"
16
17using namespace frc;
18
19static const char* kFileName = "networktables.ini";
20static const double kSaveTime = 1.2;
21
22/**
23 * If we write a new networktables.ini with some sample values, test that
24 * we get those same values back using the Preference class.
25 */
26TEST(PreferencesTest, ReadPreferencesFromFile) {
27 NetworkTable::Shutdown();
28 std::remove(kFileName);
29 std::ofstream preferencesFile(kFileName);
30 preferencesFile << "[NetworkTables Storage 3.0]" << std::endl;
31 preferencesFile
32 << "string \"/Preferences/testFileGetString\"=\"Hello, preferences file\""
33 << std::endl;
34 preferencesFile << "double \"/Preferences/testFileGetInt\"=1" << std::endl;
35 preferencesFile << "double \"/Preferences/testFileGetDouble\"=0.5"
36 << std::endl;
37 preferencesFile << "double \"/Preferences/testFileGetFloat\"=0.25"
38 << std::endl;
39 preferencesFile << "boolean \"/Preferences/testFileGetBoolean\"=true"
40 << std::endl;
41 preferencesFile
42 << "double \"/Preferences/testFileGetLong\"=1000000000000000000"
43 << std::endl;
44 preferencesFile.close();
45 NetworkTable::Initialize();
46
47 Preferences* preferences = Preferences::GetInstance();
48 EXPECT_EQ("Hello, preferences file",
49 preferences->GetString("testFileGetString"));
50 EXPECT_EQ(1, preferences->GetInt("testFileGetInt"));
51 EXPECT_FLOAT_EQ(0.5, preferences->GetDouble("testFileGetDouble"));
52 EXPECT_FLOAT_EQ(0.25f, preferences->GetFloat("testFileGetFloat"));
53 EXPECT_TRUE(preferences->GetBoolean("testFileGetBoolean"));
54 EXPECT_EQ(1000000000000000000ll, preferences->GetLong("testFileGetLong"));
55}
56
57/**
58 * If we set some values using the Preferences class, test that they show up
59 * in networktables.ini
60 */
61TEST(PreferencesTest, WritePreferencesToFile) {
62 NetworkTable::Shutdown();
63 NetworkTable::GlobalDeleteAll();
64 // persistent keys don't get deleted normally, so make remaining keys
65 // non-persistent and delete them too
66 for (const auto& info : nt::GetEntryInfo("", 0)) {
67 nt::SetEntryFlags(info.name, 0);
68 }
69 NetworkTable::GlobalDeleteAll();
70 std::remove(kFileName);
71 NetworkTable::Initialize();
72 Preferences* preferences = Preferences::GetInstance();
73 preferences->PutString("testFilePutString", "Hello, preferences file");
74 preferences->PutInt("testFilePutInt", 1);
75 preferences->PutDouble("testFilePutDouble", 0.5);
76 preferences->PutFloat("testFilePutFloat", 0.25f);
77 preferences->PutBoolean("testFilePutBoolean", true);
78 preferences->PutLong("testFilePutLong", 1000000000000000000ll);
79 preferences->Save();
80
81 Wait(kSaveTime);
82
83 static char const* kExpectedFileContents[] = {
84 "[NetworkTables Storage 3.0]",
85 "boolean \"/Preferences/testFilePutBoolean\"=true",
86 "double \"/Preferences/testFilePutDouble\"=0.5",
87 "double \"/Preferences/testFilePutFloat\"=0.25",
88 "double \"/Preferences/testFilePutInt\"=1",
89 "double \"/Preferences/testFilePutLong\"=1e+18",
90 "string \"/Preferences/testFilePutString\"=\"Hello, preferences file\""};
91
92 std::ifstream preferencesFile(kFileName);
93 for (auto& kExpectedFileContent : kExpectedFileContents) {
94 ASSERT_FALSE(preferencesFile.eof())
95 << "Preferences file prematurely reached EOF";
96
97 std::string line;
98 std::getline(preferencesFile, line);
99
100 ASSERT_EQ(kExpectedFileContent, line)
101 << "A line in networktables.ini was not correct";
102 }
103}