浏览代码

added a unit test and a comment

wangyu- 8 年之前
父节点
当前提交
d2ebcf6f6d
共有 2 个文件被更改,包括 13 次插入1 次删除
  1. 2 1
      common.cpp
  2. 11 0
      main.cpp

+ 2 - 1
common.cpp

@@ -302,7 +302,7 @@ void setnonblocking(int sock) {
 /*
 /*
     Generic checksum calculation function
     Generic checksum calculation function
 */
 */
-unsigned short csum(const unsigned short *ptr,int nbytes) {
+unsigned short csum(const unsigned short *ptr,int nbytes) {//works both for big and little endian
     register long sum;
     register long sum;
     unsigned short oddbyte;
     unsigned short oddbyte;
     register short answer;
     register short answer;
@@ -325,6 +325,7 @@ unsigned short csum(const unsigned short *ptr,int nbytes) {
     return(answer);
     return(answer);
 }
 }
 
 
+
 int set_buf_size(int fd)
 int set_buf_size(int fd)
 {
 {
     if(setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
     if(setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)

+ 11 - 0
main.cpp

@@ -2628,6 +2628,17 @@ int unit_test()
 		}
 		}
 		printf("\n");
 		printf("\n");
 	}
 	}
+
+	char s1[]={1,2,3,4,5};
+
+	char s2[]={1};
+
+	short c1=csum((unsigned short*)s1,5);
+	short c2=csum((unsigned short*)s2,1);
+	//c2=0;
+
+	printf("%x %x\n",(int)c1,(int)c2);
+
 	return 0;
 	return 0;
 }
 }