xref: /openbmc/openbmc-test-automation/lib/list_utils.robot (revision 8bd280d377930113061314ca18769d89a127ec67)
1*** Settings ***
2Documentation  This module contains keywords for list manipulation.
3
4Library  Collections
5
6*** Keywords ***
7
8Smart Combine Lists
9    [Documentation]  Combine all valid list arguments and return the result.
10    [Arguments]  @{lists}
11
12    # Description of argument(s):
13    # lists  A list of lists to be combined.  Any item in this list which is
14    #        NOT a list will be removed.
15
16    ${list_size}=  Get Length  ${lists}
17    ${index}=  Set Variable  ${0}
18
19    FOR  ${arg}  IN  @{lists}
20      ${type_arg}=  Evaluate  str(type($lists[${index}])).split("'")[1]
21      IF  '${type_arg}' != 'list'
22           Remove From List  ${lists}  ${index}
23           Continue For Loop
24      END
25      ${index}=  Evaluate  ${index}+1
26    END
27
28    ${new_list}=  Combine Lists  @{lists}
29
30    RETURN  ${new_list}
31
32
33Intersect Lists
34    [Documentation]  Intersects the two lists passed in. Returns a list of
35    ...  values common to both lists with no duplicates.
36    [Arguments]  ${list1}  ${list2}
37
38    # Description of argument(s):
39    # list1      The first list to intersect.
40    # list2      The second list to intersect.
41
42    ${length1}=  Get Length  ${list1}
43    ${length2}=  Get Length  ${list2}
44
45    @{intersected_list}  Create List
46
47    @{larger_list}=  Set Variable If  ${length1} >= ${length2}  ${list1}
48    ...                               ${length1} < ${length2}  ${list2}
49    @{smaller_list}=  Set Variable If  ${length1} >= ${length2}  ${list2}
50    ...                                ${length1} < ${length2}  ${list1}
51
52    FOR  ${element}  IN  @{larger_list}
53      ${rc}=  Run Keyword and Return Status  List Should Contain Value  ${smaller_list}  ${element}
54      IF  '${rc}' == 'True'  Append to List  ${intersected_list}  ${element}
55    END
56
57    @{intersected_list}=  Remove Duplicates  ${intersected_list}
58
59    RETURN  @{intersected_list}
60
61
62Subtract Lists
63    [Documentation]  Subtract list 2 from list 1 and return the result.
64    #  Return list contain items from the list 1 which are not present
65    #  in the list 2.
66    [Arguments]  ${list1}  ${list2}
67    # Description of argument(s):
68    # list1      The base list which is to be subtracted from.
69    # list2      The list which is to be subtracted from list1.
70
71    ${diff_list}=  Create List
72    FOR  ${item}  IN  @{list1}
73        ${status}=  Run Keyword And Return Status
74        ...  Should Contain  ${list2}  ${item}
75        IF  '${status}' == '${False}'  Append To List  ${diff_list}  ${item}
76    END
77
78    RETURN  ${diff_list}
79