struct block *next;
};
-size_t block_end(struct block**);
void block_free(struct block*);
int block_init(struct block**);
size_t block_length(struct block*);
int block_merkle_layer(struct block*,struct block**);
int block_merkle_root(struct block*,struct block**);
int block_pad(struct block*);
+size_t block_size(struct block*);
#endif
}
size_t block_length(struct block *p) {
- size_t i = 0;
- while(p!=NULL) {
+ if((NULL==p)||(p->index<0)) { return 0; }
+
+ size_t i = 1;
+ while((p->next!=NULL)&&(p->index<p->next->index)) {
i++;
p = p->next;
}
return i;
}
+
int block_merkle_layer(struct block *p, struct block **next_layer) {
struct block *tail;
crypto_hash_sha256_state state;
return 1;
}
+
+size_t block_size(struct block *p) {
+ size_t i = 0;
+ while(p!=NULL) {
+ i++;
+ p = p->next;
+ }
+ return i;
+}
static void block_merkle_layer_basic_test();
static void block_merkle_root_basic_test();
static void block_pad_basic_test();
+static void block_size_basic_test();
int main() {
setup_env();
block_merkle_layer_basic_test();
block_merkle_root_basic_test();
block_pad_basic_test();
+ block_size_basic_test();
clean_env();
struct block *root, *p;
assert(block_init(&root)==1);
+ root->index = 0;
p = root;
- for(size_t i=0;i<10;i++) {
+ for(size_t i=1;i<11;i++) {
assert(block_init(&(p->next))==1);
+ p->next->index = i;
p = p->next;
}
block_free(root);
assert(block_init(&root)==1);
+ root->index = 0;
p = root;
size_t count = 1;
- for(size_t i=0;i<rand()%1000;i++) {
+ for(size_t i=1;i<rand()%1000;i++) {
assert(block_init(&(p->next))==1);
+ p->next->index = i;
p = p->next;
count++;
}
assert(block_init(&root)==1);
assert(block_pad(root)==1);
- assert(block_length(root)==2);
+ assert(block_size(root)==2);
p = root->next;
for(size_t i=2;i<7;i++) {
p = p->next;
}
- assert(block_length(root)==7);
+ assert(block_size(root)==7);
for(size_t i=8;i<=1024;i<<=1) {
assert(block_pad(root)==1);
- assert(block_length(root)==i);
+ assert(block_size(root)==i);
assert(block_pad(root->next)==1);
- assert(block_length(root)==i+1);
+ assert(block_size(root)==i+1);
+ }
+
+ block_free(root);
+}
+
+static void block_size_basic_test() {
+ struct block *root, *p;
+
+ assert(1==block_init(&root));
+
+ p = root;
+ size_t j = 1;
+ for(size_t i=1;i<255%rand();i++) {
+ assert(1==block_init(&(p->next)));
+ p = p->next;
+ j++;
}
+ assert(0==block_length(root));
+
+ assert(j==block_size(root));
+
block_free(root);
}
assert(memcmp(buf,expected,69)==0);
+ block_free(p->root);
+
file_free(p);
}
assert(file_init(&p,TEST_FILE_1)==1);
assert(file_hash(p,16384)==1);
assert(memcmp(p->root->hash,expected,crypto_hash_sha256_BYTES)==0);
+
assert(10001==block_length(p->piece_layers));
assert((10000*16384+21)==p->size);
assert(1==file_init(&p,TEST_FILE_2));
assert(file_hash(p,i)==1);
- assert(memcmp(p->root,expected,crypto_hash_sha256_BYTES)==0);
+ assert(memcmp(p->root->hash,expected,crypto_hash_sha256_BYTES)==0);
assert(blocks==block_length(p->piece_layers));
assert((10000*16384+26)==p->size);