bsl_test_memcpy.h 2.5 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
/***************************************************************************
 * 
 * Copyright (c) 2009 Baidu.com, Inc. All Rights Reserved
 * $Id: bsl_test_memcpy.h,v 1.2 2009/04/07 06:35:53 xiaowei Exp $ 
 * 
 **************************************************************************/
 
 
 
/**
 * @file bsl_test_memcpy.h
 * @author xiaowei(com@baidu.com)
 * @date 2009/01/06 19:01:23
 * @version $Revision: 1.2 $ 
 * @brief 
 *  
 **/


#ifndef  __BSL_TEST_MEMCPY_H_
#define  __BSL_TEST_MEMCPY_H_

#include <cassert>
#include <stdlib.h>
#include "bsl/utils/bsl_memcpy.h"
#include <stdio.h>
#include <sys/time.h>

int test1()
{
	{
		char buf[1024], ds[1024];
		for (int i=0; i<(int)sizeof(buf); ++i) {
			buf[i] = 'a';
			ds[i] = 'z'-i%26;
		}
		for (int i=0; i<1024; ++i) {
			bsl::xmemcpy(buf, ds, i);
			for (int j=0; j<i; ++j) {
				if (buf[j] != ds[j]) {
					printf("error\n");
					assert (0);
				}
			}
		}

	}

	{
		char buf[1024], ds[1024];
		for (int i=0; i<(int)sizeof(buf); ++i) {
			buf[i] = rand()%256;
			ds[i] = rand()%256;
		}
		int lefts = 11;
		int mcp = 100;
		for (int i=0; i<(int)sizeof(buf)-mcp; ++i) {
			bsl::xmemcpy(buf+i, buf+lefts+i, mcp);
			//assert (memcmp(buf+i, buf+lefts+i, mcp) == 0);
		}
	}

	{
		int buf[1024], ds[1024];
		((char *)buf)[0] = 'a';
		((char *)ds)[0] = 'b';
		int len = 9;
		((char *)buf)[len+1] = 'c';
		((char *)ds)[len+1] = 'd';
		bsl::xmemcpy(((char *)buf)+1, ((char *)ds)+1, len);
		assert ( ((char *)buf)[0] != ((char *)ds)[0] );
		assert ( ((char *)buf)[len+1] != ((char *)ds)[len+1]);
	}
	return 0;
}

int test2()
{
	char buf1[1024+1], ds1[1024+1];
	char *buf = buf1+1;
	char *ds = ds1+1;
	int loop = 1<<20;
	int siz = 130;
	for (int i=1; i<siz; ++i) {
		timeval s, e;
		gettimeofday (&s, NULL);
		for (int j=0; j<loop; ++j) {
			bsl::xmemcpy(buf, ds, i);
		}
		gettimeofday (&e, NULL);

		printf("|\t%d\t|\t%ld\t|",i, (e.tv_sec-s.tv_sec)*1000 + (e.tv_usec-s.tv_usec)/1000);

		gettimeofday (&s, NULL);
		for (int j=0; j<loop; ++j) {
			memcpy(buf, ds, i);
		}
		gettimeofday (&e, NULL);
		printf("\t%ld\t|\n", (e.tv_sec-s.tv_sec)*1000 + (e.tv_usec-s.tv_usec)/1000);
#if 0
		gettimeofday (&s, NULL);
		for (int j=0; j<loop; ++j) {
			com::copy(buf, ds, i);
		}
		gettimeofday (&e, NULL);

		printf("\t%ld\t|\n", (e.tv_sec-s.tv_sec)*1000 + (e.tv_usec-s.tv_usec)/1000);
#endif
	}

	return 0;
}





class bsl_test_fun : public CxxTest::TestSuite
{
public:
	void test_normal()
	{
		test1();
		test2();
	}
};











#endif  //__BSL_TEST_MEMCPY_H_

/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */