1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | /*============================================================================ |
6 | ** |
7 | ** Source: test1.c |
8 | ** |
9 | ** Purpose: Test that memmove correctly copies text from one buffer |
10 | ** to another even when the buffers overlap. |
11 | ** |
12 | ** |
13 | **==========================================================================*/ |
14 | |
15 | #include <palsuite.h> |
16 | |
17 | int __cdecl main(int argc, char **argv) |
18 | { |
19 | char testA[11] = "abcdefghij" ; |
20 | char testB[15] = "aabbccddeeffgg" ; |
21 | char testC[15] = "aabbccddeeffgg" ; |
22 | char testD[15] = "aabbccddeeffgg" ; |
23 | char insString[4] = "zzz" ; |
24 | char *retVal; |
25 | |
26 | if (PAL_Initialize(argc, argv)) |
27 | { |
28 | return FAIL; |
29 | } |
30 | |
31 | /* move a string onto itself */ |
32 | retVal = (char *)memmove(testA + 2, testA, 8); |
33 | if (retVal != testA + 2) |
34 | { |
35 | Fail("The return value should have been the value of the destination" |
36 | "pointer, but wasn't\n" ); |
37 | } |
38 | |
39 | /*Check the most likely error*/ |
40 | if (memcmp(testA, "ababababab" , 11) == 0) |
41 | { |
42 | Fail("memmove should have saved the characters in the region of" |
43 | " overlap between source and destination, but didn't.\n" ); |
44 | } |
45 | |
46 | if (memcmp(testA, "ababcdefgh" , 11) != 0) |
47 | { |
48 | /* not sure what exactly went wrong. */ |
49 | Fail("memmove was called on a region containing the characters" |
50 | " \"abcdefghij\". It was to move the first 8 positions to" |
51 | " the last 8 positions, giving the result \"ababcdefgh\". " |
52 | " Instead, it gave the result \"%s\".\n" , testA); |
53 | } |
54 | |
55 | /* move a string to the front of testB */ |
56 | retVal = (char *)memmove(testB, insString, 3); |
57 | if(retVal != testB) |
58 | { |
59 | Fail("memmove: The function did not return the correct " |
60 | "string.\n" ); |
61 | } |
62 | |
63 | if(memcmp(testB, "zzzbccddeeffgg" ,15) != 0) |
64 | { |
65 | Fail("memmove: The function failed to move the string " |
66 | "correctly.\n" ); |
67 | } |
68 | |
69 | |
70 | /* move a string to the middle of testC */ |
71 | retVal = (char*)memmove(testC+5, insString, 3); |
72 | if(retVal != testC+5) |
73 | { |
74 | Fail("memmove: The function did not return the correct " |
75 | "string.\n" ); |
76 | } |
77 | |
78 | if(memcmp(testC, "aabbczzzeeffgg" ,15) != 0) |
79 | { |
80 | Fail("memmove: The function failed to move the string " |
81 | "correctly.\n" ); |
82 | } |
83 | |
84 | |
85 | /* move a string to the end of testD */ |
86 | retVal = (char*)memmove(testD+11, insString, 3); |
87 | if(retVal != testD+11) |
88 | { |
89 | Fail("memmove: The function did not return the correct " |
90 | "string.\n" ); |
91 | } |
92 | |
93 | if(memcmp(testD, "aabbccddeefzzz" ,15) != 0) |
94 | { |
95 | Fail("memmove: The function failed to move the string " |
96 | "correctly.\n" ); |
97 | } |
98 | |
99 | PAL_Terminate(); |
100 | return PASS; |
101 | |
102 | } |
103 | |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | |
110 | |
111 | |
112 | |
113 | |
114 | |
115 | |
116 | |
117 | |