⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stl_algorithm测试用例.cpp

📁 STL的测试代码
💻 CPP
字号:
#include"stdafx.h"
#include<algorithm>
#include <vector>
#include <algorithm>
#include <iostream>
#include<assert.h>
#include<string.h>
#include<functional>
#include<ctype.h>
using namespace std;
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

equal_to<char> equf;
less<char> lessf;

void test_single(char *first,char *last)
{
	assert(max('0','2')=='2');
	assert(max('0','2',lessf)=='2');
	assert(min('0','2')=='0');
	assert(min('0','2',lessf)=='0');
	strcpy(first,"abcdefg");
	swap(first[0],first[1]);
	assert(strcmp(first,"bacdefg")==0);
	iter_swap(&first[0],&first[1]);
	assert(strcmp(first,"abcdefg")==0);
}

void test_find(char * first, char * last)
{
	strcpy(first,"abccefg");
	assert(*max_element(first,last)=='g');
	assert(*max_element(first,last,lessf)=='g');
	assert(*min_element(first,last)=='a');
	assert(*min_element(first,last,lessf)=='a');
	assert(equal(first, last,first));
	assert(equal(first,last,first,equf));
	assert(lexicographical_compare(first,last-1,first,last));
	assert(lexicographical_compare(first,last-1,first,last,lessf));
	assert(mismatch(first,last,first).second==last);
	assert(mismatch(first,last,first,equf).second==last);
	
	assert(find(first,last,'c')==first+2);
	assert(find_if(first,last,bind2nd(equf,'c'))==first+2);
	assert(adjacent_find(first,last)==first+2);
    assert(adjacent_find(first,last,equf)==first+2);
	assert(count(first,last,'c')==2);
	assert(count_if(first,last,bind2nd(equf,'c'))==2);
	assert(search(first,last,first+2,last,equf)==first+2);
	assert(search_n(first,last,2,'c')==first+2);
	assert(search_n(first,last,2,'c',equf)==first+2);
	assert(find_end(first,last,first+2,last)==first+2);
	assert(find_end(first,last,first+2,last,equf)==first+2);
	assert(find_first_of(first,last,first+2,last)==first+2);
	assert(find_first_of(first,last,first+2,last,equf)==first+2);
}

    size_t gen_count=0;
	void count_c(char ch)
	{
		if(ch=='c')
			++gen_count;
	}
	char gen_x()
	{
		return('x');
	}

	void test_generate(char *first,char *last,char *dest)
	{
		plus<char> plusf;
		strcpy(first,"abccefg");
		for_each(first,last,&count_c);
		assert(gen_count==2);
		generate(first,first+2,&gen_x);
		assert(strcmp(first,"xxccefg")==0);
		generate_n(first+3,last-first-3,&gen_x);
		assert(transform(first,last,dest,bind2nd(plusf,'\1'))==dest+7);
		assert(strcmp(dest,"yydyyyy")==0);
		assert(transform(first,last,"\1\1\1\2\1\1\1",dest,plusf)==dest+7);
		assert(strcmp(dest,"yydzyyy")==0);
	}

	void test_copy(char *first,char *last,char *dest)
	{
		strcpy(first,"abcdefg");
///////////////////////////////////////////////////////////////////////////////////
		
		copy(first,first+3,first+1);
       	assert(strcmp(first,"aabcefg")==0);
		copy_backward(first+2,first+5,first+3);
		assert(strcmp(first,"bcecefg")==0);

/////可能在这个版本中实现的copy 和copy_backward 的行为已经变了。不是直接拷贝了。而是换了一个实现方法。
//////////////////////////////////////		
		strcpy(first,"eaeaefg");

		fill(first,first+3,'x');
		assert(strcmp(first,"xxxaefg")==0);
		fill_n(first,2,'y');
        assert(strcmp(first,"yyxaefg")==0);
		swap_ranges(first,first+3,first+4);
		assert(strcmp(first,"efgayyx")==0);
		replace(first,last,'y','c');
		assert(strcmp(first,"efgaccx")==0);
		replace_if(first,last,bind2nd(equf,'c'),'z');
		assert(strcmp(first,"efgazzx")==0);
		replace_copy(first,last,dest,'z','c');
		assert(strcmp(dest,"efgaccx")==0);
		replace_copy_if(first,last,dest,bind2nd(equf,'z'),'y');
		assert(strcmp(dest,"efgayyx")==0);
	}

void test_mutate(char *first, char *last,char *dest)
{
	strcpy(first,"abcdefg");
	strcpy(dest,first);
	remove(first,last,'c');
	assert(strcmp(first,"abdefgg")==0);
	remove_if(first,last,bind2nd(equf,'d'));
	assert(strcmp(first,"abefggg")==0);
	remove_copy(first,last,dest,'e');
	assert(strcmp(dest,"abfgggg")==0);
	remove_copy_if(first,last,dest,bind2nd(equf,'e'));
	assert(strcmp(dest,"abfgggg")==0);

	unique(dest,dest+8);
	assert(strcmp(dest,"abfg")==0);
	unique(dest,dest+5,lessf);
	assert(strcmp(dest,"a")==0);
	unique_copy(first,last+1,dest);
	assert(strcmp(dest,"abefg")==0);
    unique_copy(first,last+1,dest,equf);
	assert(strcmp(dest,"abefg")==0);
	reverse(first,last);
	assert(strcmp(first,"gggfeba")==0);
	reverse_copy(first,last,dest);
	assert(strcmp(dest,"abefggg")==0);
	rotate(first,first+2,last);
	 
	assert(strcmp(first,"gfebagg")==0);
	rotate_copy(first,first+2,last,dest);
	assert(strcmp(dest,"ebagggf")==0);
	random_shuffle(first,last);
	
}

bool cmp_caseless(char c1,char c2)
{
	return (tolower(c1)<tolower(c2));
}

void test_order(char *first, char *last,char *dest)
{
	greater<char> greatf;
	strcpy(first,"gfedcba");
	stable_partition(first, last,bind2nd(lessf,'d'));
	assert(strcmp(first,"cbagfed")==0);
	assert(partition(first,last,bind2nd(equf,'d'))==first+1);
	
	sort(first,last);
	assert(strcmp(first,"abcdefg")==0);
	sort(first,last,greatf);
	assert(strcmp(first,"gfedcba")==0);
	partial_sort(first,first+2,last);
	assert(first[0]=='a'&&first[1]=='b');
	partial_sort(first,first+2,last,greatf);
	assert(first[0]=='g'&&first[1]=='f');
	stable_sort(first,last);
	assert(strcmp(first,"abcdefg")==0);
	rotate(first,first+2,last);
	inplace_merge(first,last-2,last);
	assert(strcmp(first,"abcdefg")==0);
	rotate(first,first+2,last);
	inplace_merge(first,last-2,last,lessf);
	assert(strcmp(first,"abcdefg")==0);

	strcpy(dest,"tuvwxyz");
	partial_sort_copy(first,last,dest,dest+1);
	assert(strcmp(dest,"auvwxyz")==0);
	partial_sort_copy(first,last,dest,dest+1,greatf);
	assert(strcmp(dest,"guvwxyz")==0);
//////////////////////////////////////////////////
//  	nth_element(...)函数,并不一定对原序列准确的排序,如果需要的话
//		应该借助与partial_sort(...)函数,他只是快速的找到所要求的
//		第N个元素。
	nth_element(first,first+2,last,greatf);
	assert(first[2]=='e');
	nth_element(first,first+2,last);
	assert(first[2]=='c');
	
		
////////////////////////////////////////////////////

	strcpy(first,"dCcbBba");
	stable_sort(first,last,&cmp_caseless);
	assert(strcmp(first,"abBbCcd")==0);

/////////////////////////////////////////////////////
//注意:使用merge(...)前,两个序列必须都是排序的。否则,无法融合。
//在这里,cmp_caseless(...)函数,是忽略大小写的做法。
	
	merge(first+5,last,first,first+5,dest,&cmp_caseless);
	assert(strcmp(dest,"abBbcCd")==0);

//////////////////////////////////////////////////////

}

void test_search(char* first, char *last)
{
	char val='c';
	strcpy(first,"abcccfg");
	assert(lower_bound(first,last,val)==first+2);
	assert(lower_bound(first,last,val,lessf)==first+2);
	assert(upper_bound(first,last,val)==first+5);
	assert(upper_bound(first,last,val,lessf)==first+5);
	assert(equal_range(first,last,val).first==first+2);
	assert(equal_range(first,last,val,lessf).second==first+5);
	assert(binary_search(first,last,val));
	assert(binary_search(first,last,val,lessf));
	assert(includes(first,last,first+3,last));
	assert(includes(first,last,first+3,last,lessf));
}

void test_set(char *first,char *last,char *dest)
{
	strcpy(first,"abccefg");
	strcpy(dest,first);
	set_union(first,first+3,first+3,last,dest);
	assert(strcmp(dest,"abcefgg")==0);
	set_union(first,first+3,first+3,last,dest,lessf);
	assert(strcmp(dest,"abcefgg")==0);
	set_intersection(first,first+3,first+3,last,dest);
	assert(strcmp(dest,"cbcefgg")==0);
	set_intersection(first,first+3,first+3,last,dest,lessf);
	assert(strcmp(dest,"cbcefgg")==0);
	set_difference(first,first+3,first+3,last,dest,lessf);
	assert(strcmp(dest,"abcefgg")==0);
	set_symmetric_difference(first,first+3,first+3,last,dest);
	assert(strcmp(dest,"abefggg")==0);
	set_symmetric_difference(first,first+3,first+3,last,dest,lessf);
	assert(strcmp(dest,"abefggg")==0);
}

void test_heap(char *first,char *last)
{
	strcpy(first,"abccefg");
	make_heap(first,last);
	assert(first[0]=='g');
    cout<<first;

	pop_heap(first,last);
	assert(last[-1]=='g'&&first[0]=='f');
	pop_heap(first,last-1,lessf);
	assert(last[-2]=='f'&&first[0]=='e');
	push_heap(first,last-1);
	assert(first[0]=='f');
	push_heap(first,last,lessf);
	assert(first[0]=='g');
	sort_heap(first,last);
	assert(strcmp(first,"abccefg")==0);
	make_heap(first,last,lessf);
	sort_heap(first,last,lessf);
    assert(strcmp(first,"abccefg")==0);

}

void test_permute(char *first,char *last)
{
	strcpy(first,"abcdefg");
	next_permutation(first,last);
	assert(strcmp(first,"abcdegf")==0);
    next_permutation(first,last,lessf);
	assert(strcmp(first,"abcdfeg")==0);
	prev_permutation(first,last);
	assert(strcmp(first,"abcdegf")==0);
	prev_permutation(first,last,lessf);
	assert(strcmp(first,"abcdefg")==0);
}

int _tmain() {

	char buf[]="abccefg";
	char dest[]="1234567";
	char *first=buf,*last=buf+7;
	test_single(first,last);
	test_find(first,last);
	test_generate(first,last,dest);
	test_copy(first,last,dest);
	test_heap(first,last);
	test_mutate(first,last,dest);
	test_search(first,last);
	test_order(first,last,dest);
	test_permute(first,last);
	test_set(first,last,dest);


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -