首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Lucene TestUtil生成随机Unicode字符串

问如何使用Lucene TestUtil生成随机Unicode字符串
EN

Stack Overflow用户
提问于 2018-09-09 21:22:47
回答 1查看 49关注 0票数 2

我正在编写一些生成随机Unicode字符串的代码。我尝试使用Lucene Test Utils生成随机Unicode字符串的代码如下

代码语言:javascript
复制
    for (int i = 0; i < 5000; i++) {
        Random random = new Random();
        final String s = TestUtil.randomUnicodeString(random, 12);
        //final String s = TestUtil.randomUnicodeString(random);Tried both
        final byte[] utf8 = new byte[s.length() * UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR];
        final int utf8Len = UnicodeUtil.UTF16toUTF8(s, 0, s.length(), utf8);
        if(utf8Len !=8)
        {
            System.out.println("$$$$");
        }
    }

所以我在lucene版本6.2.0的Lucene代码中检查了randomUnicodeString的实现

代码语言:javascript
复制
public static String randomUnicodeString(Random r, int maxLength) {
final int end = nextInt(r, 0, maxLength);
if (end == 0) {
  // allow 0 length
  return "";
}
final char[] buffer = new char[end];
randomFixedLengthUnicodeString(r, buffer, 0, buffer.length);
return new String(buffer, 0, end);

}

randomFixedLengthUnicodeString的后续代码是

代码语言:javascript
复制
 public static void randomFixedLengthUnicodeString(Random random, char[] chars, int offset, int length) {
int i = offset;
final int end = offset + length;
while(i < end) {
  final int t = random.nextInt(5);
  if (0 == t && i < length - 1) {
    // Make a surrogate pair
    // High surrogate
    chars[i++] = (char) nextInt(random, 0xd800, 0xdbff);
    // Low surrogate
    chars[i++] = (char) nextInt(random, 0xdc00, 0xdfff);
  } else if (t <= 1) {
    chars[i++] = (char) random.nextInt(0x80);
  } else if (2 == t) {
    chars[i++] = (char) nextInt(random, 0x80, 0x7ff);
  } else if (3 == t) {
    chars[i++] = (char) nextInt(random, 0x800, 0xd7ff);
  } else if (4 == t) {
    chars[i++] = (char) nextInt(random, 0xe000, 0xffff);
  }
}

}

那么,我得到的异常的原因是什么呢?

代码语言:javascript
复制
Exception in thread "main" java.lang.NoClassDefFoundError: com/carrotsearch/randomizedtesting/generators/RandomInts
at org.apache.lucene.util.TestUtil.nextInt(TestUtil.java:433)
at org.apache.lucene.util.TestUtil.randomUnicodeString(TestUtil.java:505)
at luceneLab.lab.main(lab.java:33)Caused by: java.lang.ClassNotFoundException: com.carrotsearch.randomizedtesting.generators.RandomInts
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more

我是否为生成随机字符串的方法传递了错误的参数,或者我遗漏了一些相互依赖关系?提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-14 03:44:25

正如异常所说,您的项目中缺少com/carrotsearch/randomizedtesting/generators/RandomInts。看起来TestUtil使用了com.carrotsearch.randomizedtesting,所以您需要将它作为依赖项添加到项目中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52244952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档