Skip to content

Commit

Permalink
Merge 925189c into ce1df6e
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Jul 31, 2018
2 parents ce1df6e + 925189c commit 0335b4a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
20 changes: 12 additions & 8 deletions src/check/arbitrary/CharacterArbitrary.ts
@@ -1,23 +1,27 @@
import Random from '../../random/generator/Random';
import { Stream, stream } from '../../stream/Stream';
import Arbitrary from './definition/Arbitrary';
import Shrinkable from './definition/Shrinkable';
import { integer } from './IntegerArbitrary';

/** @hidden */
function CharacterArbitrary(min: number, max: number, mapToCode: (v: number) => number = v => v) {
function CharacterArbitrary(min: number, max: number, mapToCode: (v: number) => number) {
return integer(min, max)
.map(n => String.fromCharCode(mapToCode(n)))
.noBias();
}

/** @hidden */
const preferPrintableMapper = (v: number): number => {
if (v < 95) return v + 0x20; // 0x20-0x7e
if (v <= 0x7e) return v - 95;
return v;
};

/**
* For single printable ascii characters - char code between 0x20 (included) and 0x7e (included)
* @see https://www.ascii-code.com/
*/
function char(): Arbitrary<string> {
// Only printable characters: https://www.ascii-code.com/
return CharacterArbitrary(0x20, 0x7e);
return CharacterArbitrary(0x20, 0x7e, v => v);
}

/**
Expand Down Expand Up @@ -49,7 +53,7 @@ function base64(): Arbitrary<string> {
* For single ascii characters - char code between 0x00 (included) and 0x7f (included)
*/
function ascii(): Arbitrary<string> {
return CharacterArbitrary(0x00, 0x7f);
return CharacterArbitrary(0x00, 0x7f, preferPrintableMapper);
}

/**
Expand All @@ -61,7 +65,7 @@ function ascii(): Arbitrary<string> {
* Indeed values within 0xd800 and 0xdfff constitute surrogate pair characters and are illegal without their paired character.
*/
function char16bits(): Arbitrary<string> {
return CharacterArbitrary(0x0000, 0xffff);
return CharacterArbitrary(0x0000, 0xffff, preferPrintableMapper);
}

/**
Expand All @@ -75,7 +79,7 @@ function unicode(): Arbitrary<string> {
// You can refer to 'fromCharCode' documentation for more details
const gapSize = 0xdfff + 1 - 0xd800;
function mapping(v: number) {
if (v < 0xd800) return v;
if (v < 0xd800) return preferPrintableMapper(v);
return v + gapSize;
}
return CharacterArbitrary(0x0000, 0xffff - gapSize, mapping);
Expand Down
10 changes: 7 additions & 3 deletions test/unit/check/arbitrary/CharacterArbitrary.spec.ts
Expand Up @@ -13,9 +13,13 @@ import {

import * as genericHelper from './generic/GenericArbitraryHelper';

import * as stubRng from '../../stubs/generators';

const isStrictlySmallerCharacter = (c1: string, c2: string) => c1.codePointAt(0)! < c2.codePointAt(0)!;
const remapCharToIndex = (c: string): number => {
const cp = c.codePointAt(0)!;
if (cp >= 0x20 && cp <= 0x7e) return cp - 0x20;
if (cp < 0x20) return cp + 0x7e - 0x20;
return cp;
};
const isStrictlySmallerCharacter = (c1: string, c2: string) => remapCharToIndex(c1) < remapCharToIndex(c2);

describe('CharacterArbitrary', () => {
describe('char [single printable character]', () => {
Expand Down

0 comments on commit 0335b4a

Please sign in to comment.