View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for NativeSet.
23   *
24   * @author Ronald Brill
25   */
26  public class NativeSetTest extends WebDriverTestCase {
27  
28      /**
29       * @throws Exception if the test fails
30       */
31      @Test
32      @Alerts("1,9")
33      public void intersection() throws Exception {
34          final String html = DOCTYPE_HTML
35              + "<html><head></head>\n"
36              + "<body>\n"
37              + "<script>\n"
38              + LOG_TITLE_FUNCTION
39              + "let odds = new Set([1, 3, 5, 7, 9]);\n"
40              + "let squares = new Set([1, 4, 9]);\n"
41              + "let result = odds.intersection(squares);\n"
42              + "log(Array.from(result).sort().join(','));\n"
43              + "</script>\n"
44              + "</body></html>";
45  
46          loadPageVerifyTitle2(html);
47      }
48  
49      /**
50       * @throws Exception if the test fails
51       */
52      @Test
53      @Alerts("1,2,4,6,8,9")
54      public void union() throws Exception {
55          final String html = DOCTYPE_HTML
56              + "<html><head></head>\n"
57              + "<body>\n"
58              + "<script>\n"
59              + LOG_TITLE_FUNCTION
60              + "let evens = new Set([2, 4, 6, 8]);\n"
61              + "let squares = new Set([1, 4, 9]);\n"
62              + "let result = evens.union(squares);\n"
63              + "log(Array.from(result).sort().join(','));\n"
64              + "</script>\n"
65              + "</body></html>";
66  
67          loadPageVerifyTitle2(html);
68      }
69  
70      /**
71       * @throws Exception if the test fails
72       */
73      @Test
74      @Alerts("3,5,7")
75      public void difference() throws Exception {
76          final String html = DOCTYPE_HTML
77              + "<html><head></head>\n"
78              + "<body>\n"
79              + "<script>\n"
80              + LOG_TITLE_FUNCTION
81              + "let odds = new Set([1, 3, 5, 7, 9]);\n"
82              + "let squares = new Set([1, 4, 9]);\n"
83              + "let result = odds.difference(squares);\n"
84              + "log(Array.from(result).sort().join(','));\n"
85              + "</script>\n"
86              + "</body></html>";
87  
88          loadPageVerifyTitle2(html);
89      }
90  
91      /**
92       * @throws Exception if the test fails
93       */
94      @Test
95      @Alerts("1,2,6,8,9")
96      public void symmetricDifference() throws Exception {
97          final String html = DOCTYPE_HTML
98              + "<html><head></head>\n"
99              + "<body>\n"
100             + "<script>\n"
101             + LOG_TITLE_FUNCTION
102             + "let evens = new Set([2, 4, 6, 8]);\n"
103             + "let squares = new Set([1, 4, 9]);\n"
104             + "let result = evens.symmetricDifference(squares);\n"
105             + "log(Array.from(result).sort().join(','));\n"
106             + "</script>\n"
107             + "</body></html>";
108 
109         loadPageVerifyTitle2(html);
110     }
111 
112     /**
113      * @throws Exception if the test fails
114      */
115     @Test
116     @Alerts("true")
117     public void isSubsetOf() throws Exception {
118         final String html = DOCTYPE_HTML
119             + "<html><head></head>\n"
120             + "<body>\n"
121             + "<script>\n"
122             + LOG_TITLE_FUNCTION
123             + "let fours = new Set([4, 8, 12, 16]);\n"
124             + "let evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]);\n"
125             + "let result = fours.isSubsetOf(evens);\n"
126             + "log(result);\n"
127             + "</script>\n"
128             + "</body></html>";
129 
130         loadPageVerifyTitle2(html);
131     }
132 
133     /**
134      * @throws Exception if the test fails
135      */
136     @Test
137     @Alerts("true")
138     public void isSupersetOf() throws Exception {
139         final String html = DOCTYPE_HTML
140             + "<html><head></head>\n"
141             + "<body>\n"
142             + "<script>\n"
143             + LOG_TITLE_FUNCTION
144             + "let fours = new Set([4, 8, 12, 16]);\n"
145             + "let evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]);\n"
146             + "let result = evens.isSupersetOf(fours);\n"
147             + "log(result);\n"
148             + "</script>\n"
149             + "</body></html>";
150 
151         loadPageVerifyTitle2(html);
152     }
153 
154     /**
155      * @throws Exception if the test fails
156      */
157     @Test
158     @Alerts("true")
159     public void isDisjointFrom() throws Exception {
160         final String html = DOCTYPE_HTML
161             + "<html><head></head>\n"
162             + "<body>\n"
163             + "<script>\n"
164             + LOG_TITLE_FUNCTION
165             + "let primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]);\n"
166             + "let squares = new Set([1, 4, 9, 16]);\n"
167             + "let result = primes.isDisjointFrom(squares);\n"
168             + "log(result);\n"
169             + "</script>\n"
170             + "</body></html>";
171 
172         loadPageVerifyTitle2(html);
173     }
174 }