Skip to content

Instantly share code, notes, and snippets.

@geky
Created July 8, 2019 23:19
Show Gist options
  • Save geky/1b0659f172dc801624a7149966f3a091 to your computer and use it in GitHub Desktop.
Save geky/1b0659f172dc801624a7149966f3a091 to your computer and use it in GitHub Desktop.
#include "lfs.h"
#include "emubd/lfs_emubd.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// test stuff
static void test_log(const char *s, uintmax_t v) {
printf("%s: %jd\n", s, v);
}
static void test_assert(const char *file, unsigned line,
const char *s, uintmax_t v, uintmax_t e) {
test_log(s, v);
if (v != e) {
fprintf(stderr, "\033[31m%s:%u: assert %s failed with %jd, "
"expected %jd\033[0m\n", file, line, s, v, e);
exit(-2);
}
}
#define test_assert(s, v, e) test_assert(__FILE__, __LINE__, s, v, e)
// lfs declarations
lfs_t lfs;
lfs_emubd_t bd;
uintmax_t test;
#ifndef LFS_READ_SIZE
#define LFS_READ_SIZE 1
#endif
#ifndef LFS_PROG_SIZE
#define LFS_PROG_SIZE LFS_READ_SIZE
#endif
#ifndef LFS_BLOCK_SIZE
#define LFS_BLOCK_SIZE 4096
#endif
#ifndef LFS_BLOCK_COUNT
#define LFS_BLOCK_COUNT 128
#endif
#ifndef LFS_BLOCK_CYCLES
#define LFS_BLOCK_CYCLES 1024
#endif
#ifndef LFS_CACHE_SIZE
#define LFS_CACHE_SIZE 64
#endif
#ifndef LFS_LOOKAHEAD_SIZE
#define LFS_LOOKAHEAD_SIZE 16
#endif
const struct lfs_config cfg = {
.context = &bd,
.read = &lfs_emubd_read,
.prog = &lfs_emubd_prog,
.erase = &lfs_emubd_erase,
.sync = &lfs_emubd_sync,
.read_size = LFS_READ_SIZE,
.prog_size = LFS_PROG_SIZE,
.block_size = LFS_BLOCK_SIZE,
.block_count = LFS_BLOCK_COUNT,
.block_cycles = LFS_BLOCK_CYCLES,
.cache_size = LFS_CACHE_SIZE,
.lookahead_size = LFS_LOOKAHEAD_SIZE,
};
// Entry point
int main(void) {
lfs_emubd_create(&cfg, "blocks");
test = lfs_format(&lfs, &cfg);
test_assert("lfs_format", test, 0);
test = lfs_mount(&lfs, &cfg);
test_assert("lfs_mount", test, 0);
test = lfs_mkdir(&lfs, "/dir");
test_assert("lfs_mkdir", test, 0);
// Test creating an attribute
uint32_t timestamp1 = 12345678;
lfs_file_t file;
struct lfs_file_config config;
struct lfs_attr sAttr;
memset(&config, 0, sizeof(config));
memset(&file, 0, sizeof(file));
sAttr.type = 1;
sAttr.buffer = &timestamp1;
sAttr.size = sizeof(timestamp1);
config.attr_count = 1;
config.attrs = &sAttr;
test = lfs_file_opencfg(&lfs, &file, "/dir/f1",
LFS_O_RDWR | LFS_O_CREAT | LFS_O_EXCL, &config);
test_assert("lfs_file_opencfg", test, 0);
uint32_t result;
test = lfs_getattr(&lfs, "/dir/f1", 1, &result, sizeof(result));
test_assert("lfs_getattr", test, LFS_ERR_NOATTR);
test = lfs_file_close(&lfs, &file);
test_assert("lfs_file_close", test, 0);
test = lfs_getattr(&lfs, "/dir/f1", 1, &result, sizeof(result));
test_assert("lfs_getattr", test, sizeof(result));
test_assert("result", result, timestamp1);
// Test creating an unrelated file with a different attribute
uint32_t timestamp2 = 87654321;
memset(&config, 0, sizeof(config));
memset(&file, 0, sizeof(file));
sAttr.type = 1;
sAttr.buffer = &timestamp2;
sAttr.size = sizeof(timestamp2);
config.attr_count = 1;
config.attrs = &sAttr;
test = lfs_file_opencfg(&lfs, &file, "/f1",
LFS_O_RDWR | LFS_O_CREAT | LFS_O_EXCL, &config);
test_assert("lfs_file_opencfg", test, 0);
test = lfs_getattr(&lfs, "/f1", 1, &result, sizeof(result));
test_assert("lfs_getattr", test, LFS_ERR_NOATTR);
test = lfs_file_close(&lfs, &file);
test_assert("lfs_file_close", test, 0);
test = lfs_getattr(&lfs, "/f1", 1, &result, sizeof(result));
test_assert("lfs_getattr", test, sizeof(result));
test_assert("result", result, timestamp2);
// And finally make sure original attribute was unmodified
test = lfs_getattr(&lfs, "/dir/f1", 1, &result, sizeof(result));
test_assert("lfs_getattr", test, sizeof(result));
test_assert("result", result, timestamp1);
test = lfs_unmount(&lfs);
test_assert("lfs_unmount", test, 0);
lfs_emubd_destroy(&cfg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment