File: fuzzer_utils.h

package info (click to toggle)
icu 78.2-1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 123,992 kB
  • sloc: cpp: 527,891; ansic: 112,789; sh: 4,983; makefile: 4,657; perl: 3,199; python: 2,933; xml: 749; sed: 36; lisp: 12
file content (42 lines) | stat: -rw-r--r-- 1,214 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// © 2019 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html

#ifndef FUZZER_UTILS_H_
#define FUZZER_UTILS_H_

#include <assert.h>

#include "unicode/locid.h"
#include "unicode/numsys.h"
#include "unicode/strenum.h"

struct IcuEnvironment {
  IcuEnvironment() {
    // nothing to initialize yet;
  }
};

const icu::Locale& GetRandomLocale(uint16_t rnd) {
  int32_t num_locales = 0;
  const icu::Locale* locales = icu::Locale::getAvailableLocales(num_locales);
  assert(num_locales > 0);
  return locales[rnd % num_locales];
}

const icu::NumberingSystem* CreateRandomNumberingSystem(uint16_t rnd, UErrorCode &status) {
  std::unique_ptr<icu::StringEnumeration> se(icu::NumberingSystem::getAvailableNames(status));
  if (U_FAILURE(status)) return nullptr;
  int32_t count = se->count(status);
  if (U_FAILURE(status)) return nullptr;
  int32_t index = rnd % count;
  se->reset(status);
  for (int32_t i = 0; i < index - 1; i++, se->next(nullptr, status)) {
      // empty
  }
  const char* name = se->next(nullptr, status);
  if (U_FAILURE(status)) return nullptr;
  return icu::NumberingSystem::createInstanceByName(name, status);
}


#endif  // FUZZER_UTILS_H_