Beispiel #1
0
void testDelete (void){

	printf("Testing delete!\n");

	List l = setUpList();
	assert(listLength(l) == START_LENGTH);

	//Delete the first element of the list
	deleteElement(l, 0);
	assert(getElement(l, 0) == 8);
	assert(listLength(l) == START_LENGTH - 1);

	//Delete an element from the middle of the list
	deleteElement(l, 1);
	assert(getElement(l, 1) == 4);
	assert(listLength(l) == START_LENGTH - 2);


	//Delete an element from the end of the list
	deleteElement(l, listLength(l) - 1);
	assert(getElement(l, 0) == 8);
	assert(listLength(l) == START_LENGTH - 3);

	disposeList(l);

	printf("Deleting tests passed!\n");
}
Beispiel #2
0
void testAdd (void){

	printf("Testing add!\n");

	List l = setUpList();
	assert(listLength(l) == START_LENGTH);

	//Add one element to the beginning of the list
	addElement(l, 5, 0);
	assert(getElement(l,0) == 5);
	assert(listLength(l) == START_LENGTH + 1);

	//Add one element in the middle of the list
	addElement(l, 42, 3);
//	printList(l);
	assert(getElement(l,3) == 42);
	assert(listLength(l) == START_LENGTH + 2);


	//Add one element to the end of the list
	addElement(l, 99, listLength(l));
//	printList(l);
	assert(getElement(l,listLength(l) - 1) == 99);
	assert(listLength(l) == START_LENGTH + 3);

	disposeList(l);

	printf("Adding tests passed!\n");
}
Beispiel #3
0
void testReverse(void){

	printf("Testing reverse!\n");

	//reverse the list once...
	List l = setUpList();
	printList(l);
	reverseList(l);
	printList(l);


	assert(getElement(l, 0) == 4);
	assert(getElement(l, 1) == 7);
	assert(getElement(l, 2) == 8);
	assert(getElement(l, 3) == 1);
	assert(listLength(l) == START_LENGTH);

	//reverse it again!
	reverseList(l);
	printList(l);
	assert(getElement(l, 0) == 1);
	assert(getElement(l, 1) == 8);
	assert(getElement(l, 2) == 7);
	assert(getElement(l, 3) == 4);
	assert(listLength(l) == START_LENGTH);

	//testing that appended items are reversed as well
	append(l, 32);
	reverseList(l);
	assert(getElement(l, 0) == 32);
	assert(listLength(l) == START_LENGTH + 1);

	printf("Reversing passed!\n");
}
Beispiel #4
0
void testAppend (void){

	printf("Testing Append!\n");

	List l = setUpList();
	assert(listLength(l) == START_LENGTH);

	//Append a couple of times, testing length and last
	//element each time

	append(l, 77);
	assert(getElement(l, listLength(l) - 1) == 77);
	assert(listLength(l) == START_LENGTH + 1);

	append(l, 88);
	assert(getElement(l, listLength(l) - 1) == 88);
	assert(listLength(l) == START_LENGTH + 2);

	append(l, 99);
	assert(getElement(l, listLength(l) - 1) == 99);
	assert(listLength(l) == START_LENGTH + 3);	

//	printList(l);

	printf("Appending tests passed!\n");
}
	void ArchiveEditable::openAsNew(const _TCHAR *fn, const char *key)
	{
		File::open(fn, File::NEW);
		setKey(key);
		if(iv_) index_iv_ = *iv_;
		if(key) crypt_state_ = CRYPT;
		else crypt_state_ = UNCRYPT;
		setAlign(2);
		setUpList();
	}
Beispiel #6
0
void testZip (void){
	printf("Testing Zip!\n");

	List l = setUpList();
	List i = setUpList();

	assert(listLength(l) == START_LENGTH);
	assert(listLength(i) == START_LENGTH);

	List zipped = zipList(l,i);

	assert(listLength(zipped) == START_LENGTH * 2);
	assert(getElement(zipped, 1) == 1);
	assert(getElement(zipped, 3) == 8);
	assert(getElement(zipped, 5) == 7);
	assert(getElement(zipped, 7) == 4);
	printList(zipped);

	append(l,5);
	append(l,6);

	zipped = zipList(l, i);
	assert(listLength(zipped) == 10);
	assert(getElement(zipped, listLength(zipped) - 2) == 5);
	assert(getElement(zipped, listLength(zipped) - 1) == 6);
	printList(zipped);

	append(i,5);
	append(i,6);
	append(i,7);
	append(i,8);	

	zipped = zipList(l, i);
	assert(listLength(zipped) == 14);
	assert(getElement(zipped, listLength(zipped) - 2) == 7);
	assert(getElement(zipped, listLength(zipped) - 1) == 8);
	printList(zipped);

	printf("Zipping passed!\n");
}
	void ArchiveEditable::open(const _TCHAR *fn, const char *key, CryptState crypt_state)
	{
		File::open(fn, File::WRITE);
		setKey(key);
		if(iv_) index_iv_ = *iv_;
		if(readIndex()) {
			crypt_state_ = crypt_state;
			if(crypt_state_ == KEEP) {
				if(flags_ & FLAG_CRYPTED) crypt_state_ = CRYPT;
				else crypt_state_ = UNCRYPT;
			}
			setAlign(2);
			setUpList();
		}
		else close();
	}
	void ArchiveEditable::commit()
	{
		assert(File::good());
		bool need_update = false;
		int old_file_size = length();
		int index_size = calcIndexLen();

		if(index_size != index_size_) need_update = true;
		else {
			for(ListItr i = list_.begin(); i != list_.end(); ++i) {
				if(i->attr != EntryAttr::ALREADY) {
					need_update = true;
					break;
				}
			}
		}

		if(need_update) {
			int cur_file_size = commitEntrys(index_size);
			assert(File::good());
			commitIndex(index_size);
			assert(File::good());
			// ファイルサイズ変更
			if(old_file_size > cur_file_size) truncate(cur_file_size); // 縮んだ場合は縮める。大きくなった場合は特に操作はいらない
			assert(File::good());
			File::flush();
			assert(File::good());
			index_.clear();
			list_.clear();
			if(readIndex()) setUpList();
			else close();
		}
		else {
			list_.clear();
		}
	}