1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-22 21:23:07 +03:00
esp8266/tests/device/test_FS/test_FS.ino
Ivan Grokhotkov 082a4865fc Tests update
2016-05-10 21:52:26 +08:00

137 lines
2.7 KiB
C++

#include <ESP8266WiFi.h>
#include "FS.h"
#include <BSTest.h>
BS_ENV_DECLARE();
void setup()
{
Serial.begin(115200);
BS_RUN(Serial);
}
TEST_CASE("read-write test","[fs]")
{
REQUIRE(SPIFFS.begin());
String text = "write test";
{
File out = SPIFFS.open("/tmp.txt", "w");
REQUIRE(out);
out.print(text);
}
{
File in = SPIFFS.open("/tmp.txt", "r");
REQUIRE(in);
CHECK(in.size() == text.length());
in.setTimeout(0);
String result = in.readString();
CHECK(result == text);
}
}
TEST_CASE("A bunch of files show up in openDir, and can be removed", "[fs]")
{
REQUIRE(SPIFFS.begin());
const int n = 10;
int found[n] = {0};
for (int i = 0; i < n; ++i) {
String name = "seq_";
name += i;
name += ".txt";
File out = SPIFFS.open(name, "w");
REQUIRE(out);
out.println(i);
}
{
Dir root = SPIFFS.openDir("/");
while (root.next()) {
String fileName = root.fileName();
CHECK(fileName.indexOf("seq_") == 0);
int i = fileName.substring(4).toInt();
CHECK(i >= 0 && i < n);
found[i]++;
}
for (auto f : found) {
CHECK(f == 1);
}
}
{
Dir root = SPIFFS.openDir("/");
while (root.next()) {
String fileName = root.fileName();
CHECK(SPIFFS.remove(fileName));
}
}
}
TEST_CASE("files can be renamed", "[fs]")
{
REQUIRE(SPIFFS.begin());
{
File tmp = SPIFFS.open("/tmp1.txt", "w");
tmp.println("rename test");
}
{
CHECK(SPIFFS.rename("/tmp1.txt", "/tmp2.txt"));
File tmp2 = SPIFFS.open("/tmp2.txt", "r");
CHECK(tmp2);
}
}
TEST_CASE("FS::info works","[fs]")
{
REQUIRE(SPIFFS.begin());
FSInfo info;
CHECK(SPIFFS.info(info));
Serial.printf("Total: %u\nUsed: %u\nBlock: %u\nPage: %u\nMax open files: %u\nMax path len: %u\n",
info.totalBytes,
info.usedBytes,
info.blockSize,
info.pageSize,
info.maxOpenFiles,
info.maxPathLength
);
}
TEST_CASE("FS is empty after format","[fs]")
{
REQUIRE(SPIFFS.begin());
REQUIRE(SPIFFS.format());
Dir root = SPIFFS.openDir("/");
int count = 0;
while (root.next()) {
++count;
}
CHECK(count == 0);
}
TEST_CASE("Can reopen empty file","[fs]")
{
REQUIRE(SPIFFS.begin());
{
File tmp = SPIFFS.open("/tmp.txt", "w");
}
{
File tmp = SPIFFS.open("/tmp.txt", "w");
CHECK(tmp);
}
}
void loop()
{
}