[fastmanifest] add mechanism to peek at directories in tests

Summary: I need this to be able to access directory nodes for checksum testing.

Test Plan: passed unit tests

Reviewers: lcharignon

Reviewed By: lcharignon

Subscribers: mitrandir, mjpieters, #sourcecontrol

Differential Revision: https://phabricator.fb.com/D3140368

Tasks: 10697482

Signature: t1:3140368:1460584956:671cd08ca174795445a297ef75bee53d9f09a8ad
This commit is contained in:
Tony Tung 2016-04-18 11:32:31 -07:00
parent 508b666e12
commit a40ffad87d
3 changed files with 94 additions and 2 deletions

View File

@ -43,7 +43,7 @@ find_library(OPENSSL
target_include_directories(fastmanifest PUBLIC /opt/local/include)
target_link_libraries(fastmanifest PUBLIC ${OPENSSL})
SET(TEST_HEADER_FILES tests.h)
SET(TEST_HEADER_FILES tests.c tests.h)
add_executable(checksum_test checksum_test.c ${TEST_HEADER_FILES})
target_link_libraries(checksum_test bsearch fastmanifest)

78
fastmanifest/tests.c Normal file
View File

@ -0,0 +1,78 @@
// Copyright 2016-present Facebook. All Rights Reserved.
//
// tests.c: convenience functions for unit tests.
//
// no-check-code
#include "node.h"
#include "tree.h"
#include "tree_path.h"
#include "tests.h"
typedef struct _get_path_unfiltered_metadata_t {
node_t *node;
} get_path_unfiltered_metadata_t;
static find_path_callback_result_t get_path_unfiltered_callback(
tree_t *tree,
node_t *const root_parent,
node_t *root,
const char *name, const size_t name_sz,
tree_state_changes_t *changes,
void *context) {
get_path_unfiltered_metadata_t *metadata =
(get_path_unfiltered_metadata_t *) context;
// does the path already exist?
node_t *child = get_child_by_name(root, name, name_sz);
if (child == NULL) {
return (find_path_callback_result_t) {
FIND_PATH_NOT_FOUND, root};
}
metadata->node = child;
return (find_path_callback_result_t) {FIND_PATH_OK, root};
}
get_path_unfiltered_result_t get_path_unfiltered(
tree_t *tree,
const char *path,
const size_t path_sz) {
tree_state_changes_t changes = {0};
get_path_unfiltered_metadata_t metadata;
node_t *shadow_root = tree->shadow_root;
node_t *real_root = get_child_by_index(shadow_root, 0);
if (real_root == NULL) {
return (get_path_unfiltered_result_t) {GET_PATH_WTF, NULL};
}
find_path_result_t result =
find_path(
tree,
shadow_root,
real_root,
path, path_sz,
BASIC_WALK,
&changes,
get_path_unfiltered_callback,
&metadata);
assert(changes.size_change == 0);
assert(changes.num_leaf_node_change == 0);
assert(changes.non_arena_allocations == false);
switch (result) {
case FIND_PATH_OK:
return (get_path_unfiltered_result_t) {GET_PATH_OK, metadata.node};
case FIND_PATH_NOT_FOUND:
case FIND_PATH_CONFLICT:
// `FIND_PATH_CONFLICT` is returned if there is a leaf node where we
// expect a directory node. this is treated the same as a NOT_FOUND.
return (get_path_unfiltered_result_t) {GET_PATH_NOT_FOUND, NULL};
default:
return (get_path_unfiltered_result_t) {GET_PATH_WTF, NULL};
}
}

View File

@ -1,6 +1,6 @@
// Copyright 2016-present Facebook. All Rights Reserved.
//
// tests.h: convenience macros for unit tests.
// tests.h: convenience functions for unit tests.
//
// no-check-code
@ -10,6 +10,10 @@
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
#include "result.h"
#include "tree.h"
#define ASSERT(cond) if (!(cond)) { \
printf("failed on line %d\n", __LINE__); \
exit(37); \
@ -17,4 +21,14 @@
#define STRPLUSLEN(__str__) __str__, strlen(__str__)
typedef struct _get_path_unfiltered_result_t {
get_path_code_t code;
const node_t *node;
} get_path_unfiltered_result_t;
extern get_path_unfiltered_result_t get_path_unfiltered(
tree_t *const tree,
const char *path,
const size_t path_sz);
#endif /* #ifndef __TESTLIB_TESTS_H__ */