mirror of
https://github.com/facebook/sapling.git
synced 2024-10-11 01:07:15 +03:00
a63cbaaed1
Summary: It's a small C utility. It should belong to `clib` directory. Also, `#include "../foo"` does not seem to be a good pattern. It makes include files harder to follow and make code movement more difficult. Since `clib` is already included in `-I` during compilation, remove `../`. Test Plan: `make clean local`. It still builds. Reviewers: ikostia, #mercurial, rmcelroy Reviewed By: rmcelroy Subscribers: mjpieters, vsutaria Differential Revision: https://phabricator.intern.facebook.com/D5113236 Signature: t1:5113236:1495562436:e8d64083ab0417c67b63223a092470739f4c1176
73 lines
1.8 KiB
C
73 lines
1.8 KiB
C
// Copyright 2016-present Facebook. All Rights Reserved.
|
|
//
|
|
// tests.h: convenience functions for unit tests.
|
|
//
|
|
// no-check-code
|
|
|
|
#ifndef __TESTLIB_TESTS_H__
|
|
#define __TESTLIB_TESTS_H__
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "node.h"
|
|
#include "portability/portability.h"
|
|
#include "result.h"
|
|
#include "tree.h"
|
|
|
|
#define ASSERT(cond) if (!(cond)) { \
|
|
printf("failed on line %d\n", __LINE__); \
|
|
exit(37); \
|
|
}
|
|
|
|
#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);
|
|
|
|
/**
|
|
* Computes a hash based on a value. It's not a great checksum, but it's enough
|
|
* for basic tests.
|
|
*/
|
|
static inline uint8_t *int2sha1hash(uint32_t value, uint8_t *sha1hash) {
|
|
for (size_t ix = 0; ix < SHA1_BYTES; ix += sizeof(value), value++) {
|
|
size_t left = SHA1_BYTES - ix;
|
|
size_t bytes_to_copy = left > sizeof(value) ? sizeof(value) : left;
|
|
memcpy(&sha1hash[ix], &value, bytes_to_copy);
|
|
}
|
|
return sha1hash;
|
|
}
|
|
|
|
typedef struct {
|
|
char *path;
|
|
size_t path_sz;
|
|
uint32_t checksum_seed;
|
|
uint8_t flags;
|
|
} add_to_tree_t;
|
|
|
|
/**
|
|
* Adds a bunch of paths to a tree.
|
|
*/
|
|
static inline void add_to_tree(tree_t *tree,
|
|
add_to_tree_t *requests, size_t request_sz) {
|
|
uint8_t buffer[CHECKSUM_BYTES];
|
|
|
|
for (size_t ix = 0; ix < request_sz; ix++) {
|
|
add_to_tree_t *request = &requests[ix];
|
|
add_update_path_result_t result = add_or_update_path(tree,
|
|
request->path, request->path_sz,
|
|
int2sha1hash(request->checksum_seed, buffer), SHA1_BYTES,
|
|
request->flags);
|
|
ASSERT(result == ADD_UPDATE_PATH_OK);
|
|
}
|
|
}
|
|
|
|
#endif /* #ifndef __TESTLIB_TESTS_H__ */
|