Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mf_wordsinstr1butnotstr2.test.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Testing mf_wordsinstr1butnotstr2 macro
4
5 <h4> SAS Macros </h4>
6 @li mf_wordsinstr1butnotstr2.sas
7 @li mp_assert.sas
8 @li mp_assertscope.sas
9
10**/
11
12/* basic test, with scope check */
13%mp_assertscope(SNAPSHOT)
14%let x=%mf_wordsinstr1butnotstr2(str1=xx DOLLAR x $CHAR xxx W MONNAME
15 ,str2=ff xx x xxx xxxxxx
16);
17%mp_assertscope(COMPARE,ignorelist=x)
18
19%mp_assert(
20 iftrue=(
21 "&x"="DOLLAR $CHAR W MONNAME"
22 ),
23 desc=Checking basic string,
24 outds=work.test_results
25)
26
27/* word boundary - var1 should not match var10 or var100 */
28%mp_assert(
29 iftrue=(
30 "%mf_wordsinstr1butnotstr2(str1=var1 var10 var100,str2=var1)"
31 ="var10 var100"
32 ),
33 desc=Checking word boundaries,
34 outds=work.test_results
35)
36
37/* case sensitivity - dollar does not match DOLLAR */
38%mp_assert(
39 iftrue=(
40 "%mf_wordsinstr1butnotstr2(str1=DOLLAR dollar,str2=DOLLAR)"="dollar"
41 ),
42 desc=Checking case sensitivity,
43 outds=work.test_results
44)
45
46/* duplicate words in str1 are preserved */
47%mp_assert(
48 iftrue=(
49 "%mf_wordsinstr1butnotstr2(str1=a a b a,str2=b)"="a a a"
50 ),
51 desc=Checking duplicate words,
52 outds=work.test_results
53)
54
55/* when all words match, nothing is returned */
56%mp_assert(
57 iftrue=(
58 "%mf_wordsinstr1butnotstr2(str1=a b,str2=b a)"=""
59 ),
60 desc=Checking empty result,
61 outds=work.test_results
62)
63
64/* when str1 is empty, nothing is returned */
65%mp_assert(
66 iftrue=(
67 "%mf_wordsinstr1butnotstr2(str1=,str2=a b)"=""
68 ),
69 desc=Checking empty str1,
70 outds=work.test_results
71)
72
73/* when str2 is empty, all of str1 is returned */
74%mp_assert(
75 iftrue=(
76 "%mf_wordsinstr1butnotstr2(str1=a b c,str2=)"="a b c"
77 ),
78 desc=Checking empty str2 returns all words,
79 outds=work.test_results
80)
81
82/* build strings containing 1000 variables */
83/* str2 is kept to 100 words to avoid excessive macro iterations */
84data _null_;
85 length str1 str2 expected $32767;
86 do i=1 to 1000;
87 word=cats('var',i);
88 str1=catx(' ',str1,word);
89 if mod(i,10)=0 then str2=catx(' ',str2,word);
90 else expected=catx(' ',expected,word);
91 end;
92 call symputx('str1',str1);
93 call symputx('str2',str2);
94 call symputx('expected',expected);
95run;
96
97%mp_assertscope(SNAPSHOT)
98%let result=%mf_wordsinstr1butnotstr2(str1=&str1,str2=&str2);
99%mp_assertscope(COMPARE,ignorelist=result)
100
101%let count=%sysfunc(countw(&result));
102
103%mp_assert(
104 iftrue=(
105 "&count"="900"
106 ),
107 desc=Checking 1000 variable string returns 900 words,
108 outds=work.test_results
109)
110
111%mp_assert(
112 iftrue=(
113 "&result"="&expected"
114 ),
115 desc=Checking 1000 variable string content,
116 outds=work.test_results
117)