< prev index next >

test/jdk/java/lang/ScopedValue/ManyBindings.java

Print this page

  1 /*
  2  * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */

107         // create a Carrier to bind/rebind some of the scoped values
108         int len = array.length;
109         Carrier carrier = null;
110 
111         KeyAndValue<Integer>[] newArray = Arrays.copyOf(array, len);
112         int n = Math.max(1, RND.nextInt(len / 2));
113         while (n > 0) {
114             int index = RND.nextInt(len);
115             ScopedValue<Integer> key = array[index].key;
116             int newValue = RND.nextInt();
117             if (carrier == null) {
118                 carrier = ScopedValue.where(key, newValue);
119             } else {
120                 carrier = carrier.where(key, newValue);
121             }
122             newArray[index] = new KeyAndValue<>(key, newValue);
123             n--;
124         }
125 
126         // invoke recursively
127         carrier.run(() -> {
128             test(newArray, depth+1);
129         });
130 
131         // check that the scoped values have the original values
132         check(array);
133     }
134 
135     /**
136      * Check that the given scoped values have the expected value.
137      */
138     private void check(KeyAndValue<Integer>[] array) {
139         for (int i = 0; i < array.length; i++) {
140             ScopedValue<Integer> key = array[i].key;
141             Integer value = array[i].value;
142             if (value == null) {
143                 assertFalse(key.isBound());
144             } else {
145                 assertEquals(value, key.get());
146             }
147         }

  1 /*
  2  * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */

107         // create a Carrier to bind/rebind some of the scoped values
108         int len = array.length;
109         Carrier carrier = null;
110 
111         KeyAndValue<Integer>[] newArray = Arrays.copyOf(array, len);
112         int n = Math.max(1, RND.nextInt(len / 2));
113         while (n > 0) {
114             int index = RND.nextInt(len);
115             ScopedValue<Integer> key = array[index].key;
116             int newValue = RND.nextInt();
117             if (carrier == null) {
118                 carrier = ScopedValue.where(key, newValue);
119             } else {
120                 carrier = carrier.where(key, newValue);
121             }
122             newArray[index] = new KeyAndValue<>(key, newValue);
123             n--;
124         }
125 
126         // invoke recursively
127         ScopedValue.runWhere(carrier, () -> {
128             test(newArray, depth+1);
129         });
130 
131         // check that the scoped values have the original values
132         check(array);
133     }
134 
135     /**
136      * Check that the given scoped values have the expected value.
137      */
138     private void check(KeyAndValue<Integer>[] array) {
139         for (int i = 0; i < array.length; i++) {
140             ScopedValue<Integer> key = array[i].key;
141             Integer value = array[i].value;
142             if (value == null) {
143                 assertFalse(key.isBound());
144             } else {
145                 assertEquals(value, key.get());
146             }
147         }
< prev index next >