ExperimentalTestDataProvider.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 import java.util.Random;
34 
36 public final class ExperimentalTestDataProvider {
37  private static final Random RANDOM = new Random(100);
38 
41  private final int stringLength;
42 
44  this.stringLength = stringLength;
45  }
46 
47  public double getDouble() {
48  double value = 0.0;
49  while (Double.compare(0.0, value) == 0) {
50  value = RANDOM.nextDouble();
51  }
52  return value;
53  }
54 
55  public float getFloat() {
56  float value = 0.0f;
57  while (Float.compare(0.0f, value) == 0) {
58  value = RANDOM.nextFloat();
59  }
60  return value;
61  }
62 
63  public long getLong() {
64  return varint64s.getLong();
65  }
66 
67  public int getInt() {
68  return varint32s.getInt();
69  }
70 
71  public boolean getBool() {
72  return true;
73  }
74 
75  public int getEnum() {
76  return Math.abs(getInt()) % 3;
77  }
78 
79  public String getString() {
80  StringBuilder builder = new StringBuilder(stringLength);
81  for (int i = 0; i < stringLength; ++i) {
82  builder.append((char) (RANDOM.nextInt('z' - 'a') + 'a'));
83  }
84  return builder.toString();
85  }
86 
87  public ByteString getBytes() {
88  return ByteString.copyFromUtf8(getString());
89  }
90 
95  private static final class Varint32Provider {
96  private static final int[][] VALUES = {
97  new int[] {1, 50, 100, 127}, // 1 byte values
98  new int[] {128, 500, 10000, 16383}, // 2 bytes values
99  new int[] {16384, 50000, 1000000, 2097151}, // 3 bytes values
100  new int[] {2097152, 10000000, 200000000, 268435455}, // 4 bytes values
101  new int[] {268435456, 0x30000000, 0x7FFFFFFF, 0xFFFFFFFF} // 5 bytes values
102  };
103 
105  private static final int[] NUM_SAMPLES = {3, 2, 1, 1, 2};
106 
111  private int listIndex;
112 
114  private int sampleIndex;
115 
117  private int samplesTaken;
118 
119  public int getInt() {
121  // Done taking samples from this list. Go to the next one.
122  listIndex = (listIndex + 1) % VALUES.length;
123  sampleIndex = 0;
124  samplesTaken = 0;
125  }
126 
128 
129  // All lists are exactly 4 long (i.e. power of 2), so we can optimize the mod operation
130  // with masking.
131  sampleIndex = (sampleIndex + 1) & 3;
132 
133  return value;
134  }
135  }
136 
141  private static final class Varint64Provider {
142  private static final long[][] VALUES = {
143  new long[] {1, 50, 100, 127},
144  new long[] {128, 500, 10000, 16383},
145  new long[] {16384, 50000, 1000000, 2097151},
146  new long[] {2097152, 10000000, 200000000, 268435455},
147  new long[] {268435456, 0x30000000, 0x7FFFFFFF, 34359738367L},
148  new long[] {34359738368L, 2000000000000L, 4000000000000L, 4398046511103L},
149  new long[] {4398046511104L, 200000000000000L, 500000000000000L, 562949953421311L},
150  new long[] {0x4000000000000L, 0x5000000000000L, 0x6000000000000L, 0x0FFFFFFFFFFFFFFL},
151  new long[] {0x100000000000000L, 0x3FFFFFFFFFFFFFFFL, 0x5FFFFFFFFFFFFFFL, 0x7FFFFFFFFFFFFFFFL},
152  new long[] {
153  0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL
154  }
155  };
156 
158  private static final int[] NUM_SAMPLES = {4, 2, 2, 1, 1, 1, 1, 2, 2, 4};
159 
164  private int listIndex;
165 
167  private int sampleIndex;
168 
170  private int samplesTaken;
171 
172  public long getLong() {
174  // Done taking samples from this list. Go to the next one.
175  listIndex = (listIndex + 1) % VALUES.length;
176  sampleIndex = 0;
177  samplesTaken = 0;
178  }
179 
181 
182  // All lists are exactly 4 long (i.e. power of 2), so we can optimize the mod operation
183  // with masking.
184  sampleIndex = (sampleIndex + 1) & 3;
185 
186  return value;
187  }
188  }
189 }
com.google.protobuf.ExperimentalTestDataProvider.Varint64Provider.getLong
long getLong()
Definition: ExperimentalTestDataProvider.java:172
com.google.protobuf.ExperimentalTestDataProvider.Varint64Provider.samplesTaken
int samplesTaken
Definition: ExperimentalTestDataProvider.java:170
com.google.protobuf.ExperimentalTestDataProvider.Varint32Provider.getInt
int getInt()
Definition: ExperimentalTestDataProvider.java:119
com.google.protobuf.ExperimentalTestDataProvider.Varint64Provider.listIndex
int listIndex
Definition: ExperimentalTestDataProvider.java:164
com.google.protobuf.ExperimentalTestDataProvider.varint64s
final Varint64Provider varint64s
Definition: ExperimentalTestDataProvider.java:40
com.google.protobuf.ExperimentalTestDataProvider.getFloat
float getFloat()
Definition: ExperimentalTestDataProvider.java:55
com.google.protobuf.ExperimentalTestDataProvider.Varint32Provider.sampleIndex
int sampleIndex
Definition: ExperimentalTestDataProvider.java:114
com.google.protobuf.ExperimentalTestDataProvider.RANDOM
static final Random RANDOM
Definition: ExperimentalTestDataProvider.java:37
testing::internal::Double
FloatingPoint< double > Double
Definition: gtest-internal.h:429
com.google.protobuf.ExperimentalTestDataProvider.Varint32Provider.listIndex
int listIndex
Definition: ExperimentalTestDataProvider.java:111
com.google.protobuf.ExperimentalTestDataProvider.getEnum
int getEnum()
Definition: ExperimentalTestDataProvider.java:75
testing::internal::Float
FloatingPoint< float > Float
Definition: gtest-internal.h:428
com.google.protobuf.ExperimentalTestDataProvider.varint32s
final Varint32Provider varint32s
Definition: ExperimentalTestDataProvider.java:39
com.google.protobuf.ExperimentalTestDataProvider.Varint32Provider.VALUES
static final int[][] VALUES
Definition: ExperimentalTestDataProvider.java:96
com.google.protobuf.ExperimentalTestDataProvider.getString
String getString()
Definition: ExperimentalTestDataProvider.java:79
com.google.protobuf.ExperimentalTestDataProvider.ExperimentalTestDataProvider
ExperimentalTestDataProvider(int stringLength)
Definition: ExperimentalTestDataProvider.java:43
com.google.protobuf.ExperimentalTestDataProvider.Varint32Provider
Definition: ExperimentalTestDataProvider.java:95
i
int i
Definition: gmock-matchers_test.cc:764
java
com.google.protobuf.ExperimentalTestDataProvider.getBool
boolean getBool()
Definition: ExperimentalTestDataProvider.java:71
com.google.protobuf.ExperimentalTestDataProvider.Varint64Provider.NUM_SAMPLES
static final int[] NUM_SAMPLES
Definition: ExperimentalTestDataProvider.java:158
com.google.protobuf.ExperimentalTestDataProvider.Varint32Provider.samplesTaken
int samplesTaken
Definition: ExperimentalTestDataProvider.java:117
com.google.protobuf.ExperimentalTestDataProvider.getInt
int getInt()
Definition: ExperimentalTestDataProvider.java:67
com.google.protobuf.ExperimentalTestDataProvider.Varint64Provider
Definition: ExperimentalTestDataProvider.java:141
com.google.protobuf.ExperimentalTestDataProvider.stringLength
final int stringLength
Definition: ExperimentalTestDataProvider.java:41
com.google.protobuf.ExperimentalTestDataProvider.getDouble
double getDouble()
Definition: ExperimentalTestDataProvider.java:47
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
com.google.protobuf.ExperimentalTestDataProvider.getBytes
ByteString getBytes()
Definition: ExperimentalTestDataProvider.java:87
com.google.protobuf.ExperimentalTestDataProvider.getLong
long getLong()
Definition: ExperimentalTestDataProvider.java:63
com.google.protobuf.ExperimentalTestDataProvider.Varint64Provider.sampleIndex
int sampleIndex
Definition: ExperimentalTestDataProvider.java:167
com.google.protobuf.ExperimentalTestDataProvider.Varint64Provider.VALUES
static final long[][] VALUES
Definition: ExperimentalTestDataProvider.java:142
com.google.protobuf.ExperimentalTestDataProvider.Varint32Provider.NUM_SAMPLES
static final int[] NUM_SAMPLES
Definition: ExperimentalTestDataProvider.java:105
com.google.protobuf.ExperimentalTestDataProvider
Definition: ExperimentalTestDataProvider.java:36
com.google.protobuf.ByteString
Definition: ByteString.java:67


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:51