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