Add x86_64-linux-gnu emulator

I wanted a tiny scriptable meltdown proof way to run userspace programs
and visualize how program execution impacts memory. It helps to explain
how things like Actually Portable Executable works. It can show you how
the GCC generated code is going about manipulating matrices and more. I
didn't feel fully comfortable with Qemu and Bochs because I'm not smart
enough to understand them. I wanted something like gVisor but with much
stronger levels of assurances. I wanted a single binary that'll run, on
all major operating systems with an embedded GPL barrier ZIP filesystem
that is tiny enough to transpile to JavaScript and run in browsers too.

https://justine.storage.googleapis.com/emulator625.mp4
This commit is contained in:
Justine Tunney 2020-08-25 04:23:25 -07:00
parent 467504308a
commit f4f4caab0e
1052 changed files with 65667 additions and 7825 deletions

View file

@ -5,12 +5,14 @@ PKGS += TEST_APE_LIB
TEST_APE_LIB_SRCS := $(wildcard test/ape/lib/*.c)
TEST_APE_LIB_SRCS_TEST = $(filter %_test.c,$(TEST_APE_LIB_SRCS))
TEST_APE_LIB_COMS = $(TEST_APE_LIB_OBJS:%.o=%.com)
TEST_APE_LIB_OBJS = \
$(TEST_APE_LIB_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_APE_LIB_SRCS:%.c=o/$(MODE)/%.o)
TEST_APE_LIB_COMS = \
$(TEST_APE_LIB_SRCS:%.c=o/$(MODE)/%.com)
TEST_APE_LIB_BINS = \
$(TEST_APE_LIB_COMS) \
$(TEST_APE_LIB_COMS:%=%.dbg)

View file

@ -5,13 +5,15 @@ PKGS += TEST_DSP_CORE
TEST_DSP_CORE_SRCS := $(wildcard test/dsp/core/*.c)
TEST_DSP_CORE_SRCS_TEST = $(filter %_test.c,$(TEST_DSP_CORE_SRCS))
TEST_DSP_CORE_COMS = $(TEST_DSP_CORE_OBJS:%.o=%.com)
TEST_DSP_CORE_BINS = $(TEST_DSP_CORE_COMS) $(TEST_DSP_CORE_COMS:%=%.dbg)
TEST_DSP_CORE_OBJS = \
$(TEST_DSP_CORE_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_DSP_CORE_SRCS:%.c=o/$(MODE)/%.o)
TEST_DSP_CORE_COMS = \
$(TEST_DSP_CORE_SRCS:%.c=o/$(MODE)/%.com)
TEST_DSP_CORE_TESTS = \
$(TEST_DSP_CORE_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)

View file

@ -67,7 +67,7 @@ TEST(magikarp, testConvolve2) {
cDecimate2xUint8x8(256,
tgc(tunbing(u" ☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼"
u" !“#$%&()*+,-./0123456789:;<=>⁇"
u"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_"
u"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
u"`abcdefghijklmnopqrstuvwxyz{|}~⌂"
u"ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥€ƒ"
u"áíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐"

View file

@ -5,13 +5,15 @@ PKGS += TEST_DSP_SCALE
TEST_DSP_SCALE_SRCS := $(wildcard test/dsp/scale/*.c)
TEST_DSP_SCALE_SRCS_TEST = $(filter %_test.c,$(TEST_DSP_SCALE_SRCS))
TEST_DSP_SCALE_COMS = $(TEST_DSP_SCALE_OBJS:%.o=%.com)
TEST_DSP_SCALE_BINS = $(TEST_DSP_SCALE_COMS) $(TEST_DSP_SCALE_COMS:%=%.dbg)
TEST_DSP_SCALE_OBJS = \
$(TEST_DSP_SCALE_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_DSP_SCALE_SRCS:%.c=o/$(MODE)/%.o)
TEST_DSP_SCALE_COMS = \
$(TEST_DSP_SCALE_SRCS:%.c=o/$(MODE)/%.com)
TEST_DSP_SCALE_TESTS = \
$(TEST_DSP_SCALE_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)

View file

@ -23,7 +23,7 @@
struct TtyRgb res;
TEST(rgb2ansi, testDesaturatedPurple_isQuantizedBetterThanEuclideanDistance) {
ttyquantinit(kTtyQuantXterm256, kTtyQuantRgb, kTtyBlocksUnicode);
ttyquantsetup(kTtyQuantXterm256, kTtyQuantRgb, kTtyBlocksUnicode);
/*
* the challenge to the xterm256 palette is that it was likely

View file

@ -5,13 +5,15 @@ PKGS += TEST_DSP_TTY
TEST_DSP_TTY_SRCS := $(wildcard test/dsp/tty/*.c)
TEST_DSP_TTY_SRCS_TEST = $(filter %_test.c,$(TEST_DSP_TTY_SRCS))
TEST_DSP_TTY_COMS = $(TEST_DSP_TTY_OBJS:%.o=%.com)
TEST_DSP_TTY_BINS = $(TEST_DSP_TTY_COMS) $(TEST_DSP_TTY_COMS:%=%.dbg)
TEST_DSP_TTY_OBJS = \
$(TEST_DSP_TTY_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_DSP_TTY_SRCS:%.c=o/$(MODE)/%.o)
TEST_DSP_TTY_COMS = \
$(TEST_DSP_TTY_SRCS:%.c=o/$(MODE)/%.com)
TEST_DSP_TTY_TESTS = \
$(TEST_DSP_TTY_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)

View file

@ -32,7 +32,7 @@ static const struct TtyRgb kRed = {0xff, 0, 0, 196};
char vtbuf[128];
void ttyraster_true_setup(void) {
ttyquantinit(kTtyQuantTrue, kTtyQuantRgb, kTtyBlocksUnicode);
ttyquantsetup(kTtyQuantTrue, kTtyQuantRgb, kTtyBlocksUnicode);
}
void ttyraster2x2_true(void) {
@ -51,7 +51,7 @@ TEST(ttyraster, testCorner) {
}
TEST(ttyraster, testFullBlock_favorsSpace) {
ttyquantinit(kTtyQuantTrue, kTtyQuantRgb, kTtyBlocksUnicode);
ttyquantsetup(kTtyQuantTrue, kTtyQuantRgb, kTtyBlocksUnicode);
ttyraster(vtbuf,
(struct TtyRgb *)(unsigned[2][2]){
{DARKRED, DARKRED},
@ -62,7 +62,7 @@ TEST(ttyraster, testFullBlock_favorsSpace) {
}
TEST(ttyraster, testFullBlock_favorsUnicodeWhenCurrenttFgMatchesButNotBg) {
ttyquantinit(kTtyQuantTrue, kTtyQuantRgb, kTtyBlocksUnicode);
ttyquantsetup(kTtyQuantTrue, kTtyQuantRgb, kTtyBlocksUnicode);
ttyraster(vtbuf,
(struct TtyRgb *)(unsigned[2][4]){
{DARKRED, GRAY1, GRAY1, GRAY1},
@ -73,7 +73,7 @@ TEST(ttyraster, testFullBlock_favorsUnicodeWhenCurrenttFgMatchesButNotBg) {
}
TEST(ttyraster, testFullBlock_forcesSwitchBackToSpaceForRuns) {
ttyquantinit(kTtyQuantTrue, kTtyQuantRgb, kTtyBlocksUnicode);
ttyquantsetup(kTtyQuantTrue, kTtyQuantRgb, kTtyBlocksUnicode);
ttyraster(vtbuf,
(struct TtyRgb *)(unsigned[2][8]){
{DARKRED, GRAY1, GRAY1, GRAY1, GRAY1, GRAY1, GRAY1, GRAY1},
@ -86,7 +86,7 @@ TEST(ttyraster, testFullBlock_forcesSwitchBackToSpaceForRuns) {
////////////////////////////////////////////////////////////////////////////////
TEST(ttyraster_cp437, testSide) {
ttyquantinit(kTtyQuantTrue, kTtyQuantRgb, kTtyBlocksCp437);
ttyquantsetup(kTtyQuantTrue, kTtyQuantRgb, kTtyBlocksCp437);
ttyraster(vtbuf,
(const struct TtyRgb *)(unsigned[2][2]){
{DARKRED, GRAY1},
@ -99,7 +99,7 @@ TEST(ttyraster_cp437, testSide) {
////////////////////////////////////////////////////////////////////////////////
void ttyraster_xterm256_setup(void) {
ttyquantinit(kTtyQuantXterm256, kTtyQuantRgb, kTtyBlocksUnicode);
ttyquantsetup(kTtyQuantXterm256, kTtyQuantRgb, kTtyBlocksUnicode);
}
void ttyraster2x2_xterm256(void) {

View file

@ -24,23 +24,22 @@
STATIC_YOINK("realloc");
TEST(tarjan, empty_doesNothing) {
uint32_t sorted_vertices[1] = {-1u};
uint32_t edges[][2] = {{0, 0}};
uint32_t vertex_count = 0;
uint32_t edge_count = 0;
int sorted_vertices[1] = {-1};
int edges[][2] = {{0, 0}};
int vertex_count = 0;
int edge_count = 0;
tarjan(vertex_count, (void *)edges, edge_count, sorted_vertices, NULL, NULL);
ASSERT_EQ(-1u, sorted_vertices[0]);
ASSERT_EQ(-1, sorted_vertices[0]);
}
TEST(tarjan, topologicalSort_noCycles) {
enum VertexIndex { A = 0, B = 1, C = 2, D = 3 };
const char *const vertices[] = {[A] = "A", [B] = "B", [C] = "C", [D] = "D"};
uint32_t edges[][2] = {
{A /* depends on → */, B /* which must come before A */},
{A /* depends on → */, C /* which must come before A */},
{A /* depends on → */, D /* which must come before A */},
{B /* depends on → */, C /* which must come before B */},
{B /* depends on → */, D /* which must come before B */}};
int edges[][2] = {{A /* depends on → */, B /* which must come before A */},
{A /* depends on → */, C /* which must come before A */},
{A /* depends on → */, D /* which must come before A */},
{B /* depends on → */, C /* which must come before B */},
{B /* depends on → */, D /* which must come before B */}};
/*
$ tsort <<EOF
B A
@ -54,7 +53,7 @@ TEST(tarjan, topologicalSort_noCycles) {
B
A
*/
uint32_t sorted[4], components[4], componentcount;
int sorted[4], components[4], componentcount;
ASSERT_EQ(0, tarjan(ARRAYLEN(vertices), (void *)edges, ARRAYLEN(edges),
sorted, components, &componentcount));
EXPECT_EQ(C, sorted[0]);
@ -73,12 +72,11 @@ TEST(tarjan, testOneBigCycle_isDetected_weDontCareAboutOrderInsideTheCycle) {
const char *const vertices[] = {[A] = "A", [B] = "B", [C] = "C", [D] = "D"};
/* ┌─────────┐
ABCD */
uint32_t edges[][2] = {
{A /* depends on → */, B /* which must come before A */},
{B /* depends on → */, C /* which must come before B */},
{C /* depends on → */, D /* which must come before C */},
{D /* depends on → */, A /* which must come before D */}};
uint32_t sorted[4], components[4], componentcount;
int edges[][2] = {{A /* depends on → */, B /* which must come before A */},
{B /* depends on → */, C /* which must come before B */},
{C /* depends on → */, D /* which must come before C */},
{D /* depends on → */, A /* which must come before D */}};
int sorted[4], components[4], componentcount;
ASSERT_EQ(0, tarjan(ARRAYLEN(vertices), (void *)edges, ARRAYLEN(edges),
sorted, components, &componentcount));
ASSERT_EQ(1, componentcount);
@ -104,15 +102,15 @@ TEST(tarjan, testHeaders) {
[LIBC_MACROS] = "libc/macros.h",
[LIBC_MACROS_CPP] = "libc/macros-cpp.inc",
};
uint32_t edges[][2] = {
int edges[][2] = {
{LIBC_STR_STR, LIBC_BITS_BITS}, {LIBC_STR_STR, LIBC_INTEGRAL},
{LIBC_STR_STR, LIBC_KEYWORDS}, {LIBC_BITS_BITS, LIBC_DCE},
{LIBC_BITS_BITS, LIBC_INTEGRAL}, {LIBC_BITS_BITS, LIBC_KEYWORDS},
{LIBC_BITS_BITS, LIBC_MACROS}, {LIBC_MACROS, LIBC_MACROS_CPP},
};
uint32_t sorted[ARRAYLEN(vertices)];
uint32_t components[ARRAYLEN(vertices)];
uint32_t componentcount;
int sorted[ARRAYLEN(vertices)];
int components[ARRAYLEN(vertices)];
int componentcount;
ASSERT_EQ(0, tarjan(ARRAYLEN(vertices), (void *)edges, ARRAYLEN(edges),
sorted, components, &componentcount));
ASSERT_EQ(ARRAYLEN(vertices), componentcount);

View file

@ -5,13 +5,18 @@ PKGS += TEST_LIBC_ALG
TEST_LIBC_ALG_SRCS := $(wildcard test/libc/alg/*.c)
TEST_LIBC_ALG_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_ALG_SRCS))
TEST_LIBC_ALG_COMS = $(TEST_LIBC_ALG_OBJS:%.o=%.com)
TEST_LIBC_ALG_BINS = $(TEST_LIBC_ALG_COMS) $(TEST_LIBC_ALG_COMS:%=%.dbg)
TEST_LIBC_ALG_OBJS = \
$(TEST_LIBC_ALG_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_ALG_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_ALG_COMS = \
$(TEST_LIBC_ALG_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_ALG_BINS = \
$(TEST_LIBC_ALG_COMS) \
$(TEST_LIBC_ALG_COMS:%=%.dbg)
TEST_LIBC_ALG_TESTS = $(TEST_LIBC_ALG_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
TEST_LIBC_ALG_CHECKS = \
@ -49,9 +54,7 @@ o/$(MODE)/test/libc/alg/%.com.dbg: \
$(APE)
@$(APELINK)
$(TEST_LIBC_ALG_OBJS): \
$(BUILD_FILES) \
test/libc/alg/test.mk
$(TEST_LIBC_ALG_OBJS): test/libc/alg/test.mk
$(TEST_LIBC_ALG_OBJS): \
DEFAULT_CCFLAGS += \

View file

@ -17,31 +17,52 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "dsp/tty/rgb2xterm256.h"
#include "libc/bits/bits.h"
#include "libc/bits/progn.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
TEST(rgb2xterm256, test) {
EXPECT_EQ(196, rgb2xterm256v2(0xff, 0x00, 0x00)); /* red */
EXPECT_EQ(46, rgb2xterm256v2(0x00, 0xff, 0x00)); /* green */
EXPECT_EQ(21, rgb2xterm256v2(0x00, 0x00, 0xff)); /* blue */
EXPECT_EQ(226, rgb2xterm256v2(0xff, 0xff, 0x00)); /* yellow */
EXPECT_EQ(208, rgb2xterm256v2(0xff, 0x80, 0x00)); /* orange */
#define L(x) ((int64_t)(x))
#define S(x) ((int128_t)(x))
#define U(x) ((uint128_t)(x))
TEST(division, testUnsigned) {
volatile uint128_t x;
EXPECT_EQ(U(20769187431582143),
PROGN(x = U(1000000000123123123), (U(1125899906842624) << 64) / x));
EXPECT_EQ((U(26807140639110) << 64) | U(1756832768924719201),
PROGN(x = U(42), (U(1125899906842624) << 64) / x));
}
TEST(rgb2xterm256, testRedBlack) {
EXPECT_EQ(16, rgb2xterm256v2(0, 0, 0));
EXPECT_EQ(16, rgb2xterm256v2(12, 0, 0));
EXPECT_EQ(232, rgb2xterm256v2(13, 0, 0));
EXPECT_EQ(233, rgb2xterm256v2(39, 0, 0));
EXPECT_EQ(233, rgb2xterm256v2(40, 0, 0));
EXPECT_EQ(52, rgb2xterm256v2(53, 0, 0));
EXPECT_EQ(88, rgb2xterm256v2(115, 0, 0));
EXPECT_EQ(88, rgb2xterm256v2(116, 0, 0));
TEST(division, testSigned) {
volatile int128_t x;
EXPECT_EQ(S(20769187431582143),
PROGN(x = S(1000000000123123123), (S(1125899906842624) << 64) / x));
EXPECT_EQ(S(26807140639110) << 64 | S(1756832768924719201),
PROGN(x = S(42), (S(1125899906842624) << 64) / x));
}
////////////////////////////////////////////////////////////////////////////////
BENCH(rgb2xterm256v2, bench) {
EZBENCH(donothing, rgb2xterm256v2(0xff, 0x80, 0x00));
BENCH(divmodti4, bench) {
volatile int128_t x;
EZBENCH2("divmodti4 small / small", donothing,
PROGN(x = S(42), x = S(112589990684) / x));
EZBENCH2("divmodti4 small / large", donothing,
PROGN(x = U(123) << 64 | 123, x = S(112589990684) / x));
EZBENCH2("divmodti4 large / small", donothing,
PROGN(x = 123, x = (S(1125899906842624) << 64 | 334) / x));
EZBENCH2(
"divmodti4 large / large", donothing,
PROGN(x = U(123) << 64 | 123, x = (S(1125899906842624) << 64 | 334) / x));
}
BENCH(idiv32, bench) {
volatile int32_t x;
EZBENCH2("idiv32", donothing,
PROGN(x = L(1000000000123123123), x = L(1125899906842624) / x));
}
BENCH(idiv64, bench) {
volatile int64_t x;
EZBENCH2("idiv64", donothing,
PROGN(x = L(1000000000123123123), x = L(1125899906842624) / x));
}

View file

@ -17,6 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/limits.h"
#include "libc/macros.h"
#include "libc/testlib/testlib.h"

View file

@ -0,0 +1,35 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/popcnt.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
TEST(popcnt, test) {
EXPECT_EQ(32, popcnt(0x5555555555555555));
EXPECT_EQ(32, popcnt(VEIL("r", 0x5555555555555555)));
EXPECT_EQ(32, (popcnt)(VEIL("r", 0x5555555555555555)));
}
BENCH(popcnt, bench) {
EZBENCH2("popcnt", donothing,
EXPROPRIATE(popcnt(VEIL("r", 0x5555555555555555))));
EZBENCH2("(popcnt)", donothing,
EXPROPRIATE((popcnt)(VEIL("r", 0x5555555555555555))));
}

View file

@ -5,12 +5,14 @@ PKGS += TEST_LIBC_BITS
TEST_LIBC_BITS_SRCS := $(wildcard test/libc/bits/*.c)
TEST_LIBC_BITS_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_BITS_SRCS))
TEST_LIBC_BITS_COMS = $(TEST_LIBC_BITS_OBJS:%.o=%.com)
TEST_LIBC_BITS_OBJS = \
$(TEST_LIBC_BITS_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_BITS_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_BITS_COMS = \
$(TEST_LIBC_BITS_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_BITS_BINS = \
$(TEST_LIBC_BITS_COMS) \
$(TEST_LIBC_BITS_COMS:%=%.dbg)
@ -26,7 +28,8 @@ TEST_LIBC_BITS_DIRECTDEPS = \
LIBC_BITS \
LIBC_NEXGEN32E \
LIBC_STUBS \
LIBC_TESTLIB
LIBC_TESTLIB \
THIRD_PARTY_COMPILER_RT
TEST_LIBC_BITS_DEPS := \
$(call uniq,$(foreach x,$(TEST_LIBC_BITS_DIRECTDEPS),$($(x))))

View file

@ -17,8 +17,8 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/runtime/gc.h"
#include "libc/calls/internal.h"
#include "libc/runtime/gc.h"
#include "libc/testlib/testlib.h"
char16_t p[PATH_MAX];

View file

@ -7,12 +7,14 @@ TEST_LIBC_CALLS_SRCS := \
$(wildcard test/libc/calls/*.c) \
$(wildcard test/libc/calls/hefty/*.c)
TEST_LIBC_CALLS_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_CALLS_SRCS))
TEST_LIBC_CALLS_COMS = $(TEST_LIBC_CALLS_OBJS:%.o=%.com)
TEST_LIBC_CALLS_OBJS = \
$(TEST_LIBC_CALLS_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_CALLS_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_CALLS_COMS = \
$(TEST_LIBC_CALLS_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_CALLS_BINS = \
$(TEST_LIBC_CALLS_COMS) \
$(TEST_LIBC_CALLS_COMS:%=%.dbg)

View file

@ -0,0 +1,63 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/conv/itoa.h"
#include "libc/limits.h"
#include "libc/testlib/testlib.h"
TEST(int64toarray_radix10, test) {
char buf[21];
EXPECT_EQ(1, int64toarray_radix10(0, buf));
EXPECT_STREQ("0", buf);
EXPECT_EQ(19, int64toarray_radix10(INT64_MAX, buf));
EXPECT_STREQ("9223372036854775807", buf);
EXPECT_EQ(20, int64toarray_radix10(INT64_MIN, buf));
EXPECT_STREQ("-9223372036854775808", buf);
}
TEST(uint64toarray_radix10, test) {
char buf[21];
EXPECT_EQ(1, uint64toarray_radix10(0, buf));
EXPECT_STREQ("0", buf);
EXPECT_EQ(20, uint64toarray_radix10(UINT64_MAX, buf));
EXPECT_STREQ("18446744073709551615", buf);
EXPECT_EQ(19, uint64toarray_radix10(INT64_MIN, buf));
EXPECT_STREQ("9223372036854775808", buf);
}
TEST(int128toarray_radix10, test) {
char buf[41];
EXPECT_EQ(1, int128toarray_radix10(0, buf));
EXPECT_STREQ("0", buf);
EXPECT_EQ(39, int128toarray_radix10(INT128_MAX, buf));
EXPECT_STREQ("170141183460469231731687303715884105727", buf);
EXPECT_EQ(40, int128toarray_radix10(INT128_MIN, buf));
EXPECT_STREQ("-170141183460469231731687303715884105728", buf);
}
TEST(uint128toarray_radix10, test) {
char buf[40];
EXPECT_EQ(1, uint128toarray_radix10(0, buf));
EXPECT_STREQ("0", buf);
EXPECT_EQ(39, uint128toarray_radix10(UINT128_MAX, buf));
EXPECT_STREQ("340282366920938463463374607431768211455", buf);
EXPECT_EQ(39, uint128toarray_radix10(INT128_MIN, buf));
EXPECT_STREQ("170141183460469231731687303715884105728", buf);
}

View file

@ -17,22 +17,17 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/intrin/psraw.h"
#include "libc/limits.h"
#include "libc/conv/itoa.h"
#include "libc/testlib/testlib.h"
TEST(psraw, testPositive) {
short A[8] = {1, 2, SHRT_MAX};
psraw(A, A, 1);
EXPECT_EQ(0, A[0]);
EXPECT_EQ(1, A[1]);
EXPECT_EQ(SHRT_MAX / 2, A[2]);
TEST(itoa64radix16, test) {
char buf[21];
EXPECT_EQ(5, uint64toarray_radix16(0x31337, buf));
EXPECT_STREQ("31337", buf);
}
TEST(psraw, testNegative) {
short A[8] = {-1, -2, SHRT_MIN};
psraw(A, A, 1);
EXPECT_EQ(-1, A[0]);
EXPECT_EQ(-1, A[1]);
EXPECT_EQ(SHRT_MIN / 2, A[2]);
TEST(itoa64fixed16, test) {
char buf[21];
EXPECT_EQ(8, uint64toarray_fixed16(0x31337, buf, 32));
EXPECT_STREQ("00031337", buf);
}

View file

@ -21,20 +21,10 @@
#include "libc/conv/conv.h"
#include "libc/testlib/testlib.h"
/* todo(jart): work on this more */
TEST(strtoimax, testZero) {
EXPECT_EQ(0, strtoimax("0", NULL, 0));
}
TEST(strtoimax, testDecimal) {
EXPECT_EQ(-123, strtoimax("-123", NULL, 0));
}
TEST(strtoimax, testHex) {
EXPECT_EQ(-255, strtoimax("-0xff", NULL, 0));
}
TEST(strtoimax, testOctal) {
EXPECT_EQ(-123, strtoimax("-0173", NULL, 0));
}
TEST(strtoimax, testZero) { EXPECT_EQ(0, strtoimax("0", NULL, 0)); }
TEST(strtoimax, testDecimal) { EXPECT_EQ(-123, strtoimax("-123", NULL, 0)); }
TEST(strtoimax, testHex) { EXPECT_EQ(-255, strtoimax("-0xff", NULL, 0)); }
TEST(strtoimax, testOctal) { EXPECT_EQ(-123, strtoimax("-0173", NULL, 0)); }
TEST(strtoimax, testLimits) {
EXPECT_EQ(
@ -50,3 +40,8 @@ TEST(strtoimax, testZeroExtend) {
EXPECT_EQ(0xffffffff, strtoimax("-1u", NULL, 0));
EXPECT_EQ(0xffffffffffffffff, strtoimax("-1ul", NULL, 0));
}
TEST(strtoimax, testTwosBane) {
EXPECT_EQ(((uintmax_t)0x8000000000000000) << 64 | 0x0000000000000000,
strtoimax("0x80000000000000000000000000000000", NULL, 0));
}

View file

@ -5,12 +5,14 @@ PKGS += TEST_LIBC_CONV
TEST_LIBC_CONV_SRCS := $(wildcard test/libc/conv/*.c)
TEST_LIBC_CONV_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_CONV_SRCS))
TEST_LIBC_CONV_COMS = $(TEST_LIBC_CONV_OBJS:%.o=%.com)
TEST_LIBC_CONV_OBJS = \
$(TEST_LIBC_CONV_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_CONV_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_CONV_COMS = \
$(TEST_LIBC_CONV_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_CONV_BINS = \
$(TEST_LIBC_CONV_COMS) \
$(TEST_LIBC_CONV_COMS:%=%.dbg)
@ -43,9 +45,7 @@ o/$(MODE)/test/libc/conv/%.com.dbg: \
$(APE)
@$(APELINK)
$(TEST_LIBC_CONV_OBJS): \
$(BUILD_FILES) \
test/libc/conv/test.mk
$(TEST_LIBC_CONV_OBJS): test/libc/conv/test.mk
$(TEST_LIBC_CONV_OBJS): \
DEFAULT_CCFLAGS += \

View file

@ -42,10 +42,9 @@ TEST(parseresolvconf, testEmpty) {
}
TEST(parseresolvconf, testCorrectlyTokenizes) {
const char kInput[] =
"# this is a comment\n"
"nameserver 203.0.113.2 \n"
" nameserver 203.0.113.1\n";
const char kInput[] = "# this is a comment\n"
"nameserver 203.0.113.2 \n"
" nameserver 203.0.113.1\n";
struct ResolvConf *rv = calloc(1, sizeof(struct ResolvConf));
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
ASSERT_EQ(strlen(kInput), fwrite(kInput, 1, strlen(kInput), f));
@ -61,16 +60,13 @@ TEST(parseresolvconf, testCorrectlyTokenizes) {
fclose(f);
}
TEST(parseresolvconf, testSearchLocal_setsLoopback) {
TEST(parseresolvconf, testMulticastDnsThing_getsIgnored) {
const char kInput[] = "search local # boop\n";
struct ResolvConf *rv = calloc(1, sizeof(struct ResolvConf));
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
ASSERT_EQ(strlen(kInput), fwrite(kInput, 1, strlen(kInput), f));
ASSERT_EQ(1, parseresolvconf(rv, f));
ASSERT_EQ(1, rv->nameservers.i);
EXPECT_EQ(AF_INET, rv->nameservers.p[0].sin_family);
EXPECT_EQ(DNS_PORT, ntohs(rv->nameservers.p[0].sin_port));
EXPECT_STREQ("127.0.0.1", FormatIp(&rv->nameservers.p[0]));
ASSERT_EQ(0, parseresolvconf(rv, f));
ASSERT_EQ(0, rv->nameservers.i);
freeresolvconf(&rv);
fclose(f);
}

View file

@ -5,12 +5,14 @@ PKGS += TEST_LIBC_DNS
TEST_LIBC_DNS_SRCS := $(wildcard test/libc/dns/*.c)
TEST_LIBC_DNS_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_DNS_SRCS))
TEST_LIBC_DNS_COMS = $(TEST_LIBC_DNS_OBJS:%.o=%.com)
TEST_LIBC_DNS_OBJS = \
$(TEST_LIBC_DNS_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_DNS_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_DNS_COMS = \
$(TEST_LIBC_DNS_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_DNS_BINS = \
$(TEST_LIBC_DNS_COMS) \
$(TEST_LIBC_DNS_COMS:%=%.dbg)

View file

@ -27,12 +27,15 @@
#include "libc/bits/progn.h"
#include "libc/bits/pushpop.h"
#include "libc/bits/safemacros.h"
#include "libc/conv/itoa.h"
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/limits.h"
#include "libc/math.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
@ -479,10 +482,8 @@ TEST(sprintf, test_types) {
TEST(sprintf, testOverflow_truncationNotSaturation) {
errno = 0;
EXPECT_STREQ("13398", Format("%hu", 0x123456UL));
EXPECT_EQ(ERANGE, errno);
errno = 0;
EXPECT_STREQ("Test16 65535", Format("%s%hhi %hu", "Test", 10000, 0xFFFFFFFF));
EXPECT_EQ(ERANGE, errno);
}
TEST(sprintf, test_pointer) {
@ -598,6 +599,10 @@ TEST(xasprintf, test) {
free(pp);
}
TEST(xasprintf, nullPointer) {
ASSERT_STREQ("000000000000", gc(xasprintf("%p", NULL)));
}
TEST(xasprintf, pointer_doesntShowNonCanonicalZeroes) {
ASSERT_STREQ("100000000010", gc(xasprintf("%p", 0x0000100000000010)));
ASSERT_STREQ("0x100000000010", gc(xasprintf("%#p", 0x0000100000000010)));
@ -610,6 +615,19 @@ TEST(xasprintf, nonCanonicalPointer_discardsHighBits_ratherThanSaturate) {
ASSERT_STREQ("0x7fffffffffff", gc(xasprintf("%#p", 0x7fffffffffff)));
}
TEST(xasprintf, hugeNtoa) {
ASSERT_STREQ(
"0b1111111111111111111111111111111111111111111111111111111111111111111111"
"1111111111111111111111111111111111111111111111111111111111",
gc(xasprintf("%#jb", UINT128_MAX)));
}
TEST(xasprintf, twosBane) {
ASSERT_STREQ("-2147483648", gc(xasprintf("%d", 0x80000000)));
ASSERT_STREQ("-9223372036854775808",
gc(xasprintf("%ld", 0x8000000000000000)));
}
TEST(snprintf, testFixedWidthString_wontOverrunInput) {
const int N = 2;
char *buf = tmalloc(N + 1);
@ -651,3 +669,17 @@ TEST(snprintf, testFixedWidthStringIsNull_wontLeakMemory) {
EXPECT_BINEQ(u"NULL             ", buf);
tfree(buf);
}
TEST(snprintf, twosBaneWithTypePromotion) {
int16_t x = 0x8000;
EXPECT_STREQ("-32768", Format("%hd", x));
}
BENCH(palandprintf, bench) {
EZBENCH2("snprintf %x", donothing, Format("%x", VEIL("r", INT_MIN)));
EZBENCH2("snprintf %d", donothing, Format("%d", VEIL("r", INT_MIN)));
EZBENCH2("snprintf %s", donothing, Format("%s", VEIL("r", "hi (╯°□°)╯")));
EZBENCH2("snprintf %hs", donothing, Format("%hs", VEIL("r", u"hi (╯°□°)╯")));
EZBENCH2("snprintf %ls", donothing, Format("%ls", VEIL("r", L"hi (╯°□°)╯")));
EZBENCH2("int64toarray", donothing, int64toarray_radix10(-3, buffer));
}

View file

@ -40,10 +40,10 @@ TEST(SUITE(sprintf), testCharacterCounting) {
TEST(SUITE(snprintf), testTableFlip) {
EXPECT_STREQ("Table flip ", Format("%-20ls", L"Table flip"));
EXPECT_STREQ("(╯°□°)╯︵ ┻━┻ ", Format("%-20ls", L"(╯°□°)╯︵ ┻━┻"));
EXPECT_STREQ("(╯°□°)╯︵ ┻━┻ ", Format("%-20hs", u"(╯°□°)╯︵ ┻━┻"));
EXPECT_STREQ("(╯°□°)╯︵L┻━┻ ", Format("%-20ls", L"(╯°□°)╯︵L┻━┻"));
EXPECT_STREQ("(╯°□°)╯︵u┻━┻ ", Format("%-20hs", u"(╯°□°)╯︵u┻━┻"));
EXPECT_STREQ("ちゃぶ台返し ", Format("%-20ls", L"ちゃぶ台返し"));
EXPECT_STREQ(" (╯°□°)╯︵ ┻━┻", Format("%20ls", L"(╯°□°)╯︵ ┻━┻"));
EXPECT_STREQ(" (╯°□°)╯︵L┻━┻", Format("%20ls", L"(╯°□°)╯︵L┻━┻"));
EXPECT_STREQ(" ちゃぶ台返し", Format("%20ls", L"ちゃぶ台返し"));
}

View file

@ -30,6 +30,11 @@
* libc/sysv/kErrnoNames.S needs updating.
*/
TEST(strerror, e2big) {
if (IsTiny()) return;
EXPECT_STARTSWITH("E2BIG", strerror(E2BIG));
}
TEST(strerror, enosys) {
if (IsTiny()) return;
EXPECT_STARTSWITH("ENOSYS", strerror(ENOSYS));

View file

@ -5,7 +5,6 @@ PKGS += TEST_LIBC_FMT
TEST_LIBC_FMT_SRCS := $(wildcard test/libc/fmt/*.c)
TEST_LIBC_FMT_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_FMT_SRCS))
TEST_LIBC_FMT_COMS = $(TEST_LIBC_FMT_OBJS:%.o=%.com)
TEST_LIBC_FMT_BINS = $(TEST_LIBC_FMT_COMS) $(TEST_LIBC_FMT_COMS:%=%.dbg)
TEST_LIBC_FMT_TESTS = $(TEST_LIBC_FMT_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
@ -13,11 +12,15 @@ TEST_LIBC_FMT_OBJS = \
$(TEST_LIBC_FMT_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_FMT_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_FMT_COMS = \
$(TEST_LIBC_FMT_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_FMT_CHECKS = \
$(TEST_LIBC_FMT_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
TEST_LIBC_FMT_DIRECTDEPS = \
LIBC_CALLS_HEFTY \
LIBC_CONV \
LIBC_FMT \
LIBC_MEM \
LIBC_NEXGEN32E \
@ -45,9 +48,7 @@ o/$(MODE)/test/libc/fmt/%.com.dbg: \
$(APE)
@$(APELINK)
$(TEST_LIBC_FMT_OBJS): \
$(BUILD_FILES) \
test/libc/fmt/test.mk
$(TEST_LIBC_FMT_OBJS): test/libc/fmt/test.mk
.PHONY: o/$(MODE)/test/libc/fmt
o/$(MODE)/test/libc/fmt: \

File diff suppressed because it is too large Load diff

View file

@ -17,71 +17,216 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/progn.h"
#include "libc/intrin/palignr.h"
#include "libc/rand/rand.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
TEST(palignr, testLeftpad) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4] = {0};
palignr(C, B, A, 12);
EXPECT_EQ(4, C[0]);
EXPECT_EQ(5, C[1]);
EXPECT_EQ(6, C[2]);
EXPECT_EQ(7, C[3]);
ASSERT_EQ(4, C[0]);
ASSERT_EQ(5, C[1]);
ASSERT_EQ(6, C[2]);
ASSERT_EQ(7, C[3]);
}
TEST(palignr, testLeftpad_variableImmediate) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4] = {0};
palignr(C, B, A, VEIL("r", 12));
ASSERT_EQ(4, C[0]);
ASSERT_EQ(5, C[1]);
ASSERT_EQ(6, C[2]);
ASSERT_EQ(7, C[3]);
}
TEST(palignr, test0) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4];
palignr(C, B, A, 0);
EXPECT_EQ(1, C[0]);
EXPECT_EQ(2, C[1]);
EXPECT_EQ(3, C[2]);
EXPECT_EQ(4, C[3]);
ASSERT_EQ(1, C[0]);
ASSERT_EQ(2, C[1]);
ASSERT_EQ(3, C[2]);
ASSERT_EQ(4, C[3]);
}
TEST(palignr, test4) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4];
palignr(C, B, A, 4);
EXPECT_EQ(2, C[0]);
EXPECT_EQ(3, C[1]);
EXPECT_EQ(4, C[2]);
EXPECT_EQ(5, C[3]);
ASSERT_EQ(2, C[0]);
ASSERT_EQ(3, C[1]);
ASSERT_EQ(4, C[2]);
ASSERT_EQ(5, C[3]);
}
TEST(palignr, test12) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4];
palignr(C, B, A, 12);
EXPECT_EQ(4, C[0]);
EXPECT_EQ(5, C[1]);
EXPECT_EQ(6, C[2]);
EXPECT_EQ(7, C[3]);
ASSERT_EQ(4, C[0]);
ASSERT_EQ(5, C[1]);
ASSERT_EQ(6, C[2]);
ASSERT_EQ(7, C[3]);
}
TEST(palignr, test16) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4];
palignr(C, B, A, 16);
EXPECT_EQ(5, C[0]);
EXPECT_EQ(6, C[1]);
EXPECT_EQ(7, C[2]);
EXPECT_EQ(8, C[3]);
ASSERT_EQ(5, C[0]);
ASSERT_EQ(6, C[1]);
ASSERT_EQ(7, C[2]);
ASSERT_EQ(8, C[3]);
}
TEST(palignr, test20) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4] = {-1, -1, -1, -1};
palignr(C, B, A, 20);
EXPECT_EQ(6, C[0]);
EXPECT_EQ(7, C[1]);
EXPECT_EQ(8, C[2]);
EXPECT_EQ(0, C[3]);
ASSERT_EQ(6, C[0]);
ASSERT_EQ(7, C[1]);
ASSERT_EQ(8, C[2]);
ASSERT_EQ(0, C[3]);
}
TEST(palignr, test32) {
TEST(palignrc, testLeftpad) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4] = {0};
(palignr)(C, B, A, 12);
ASSERT_EQ(4, C[0]);
ASSERT_EQ(5, C[1]);
ASSERT_EQ(6, C[2]);
ASSERT_EQ(7, C[3]);
}
TEST(palignrc, testLeftpad_variableImmediate) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4] = {0};
(palignr)(C, B, A, VEIL("r", 12));
ASSERT_EQ(4, C[0]);
ASSERT_EQ(5, C[1]);
ASSERT_EQ(6, C[2]);
ASSERT_EQ(7, C[3]);
}
TEST(palignrc, test0) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4];
(palignr)(C, B, A, 0);
ASSERT_EQ(1, C[0]);
ASSERT_EQ(2, C[1]);
ASSERT_EQ(3, C[2]);
ASSERT_EQ(4, C[3]);
}
TEST(palignrc, test4) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4];
(palignr)(C, B, A, 4);
ASSERT_EQ(2, C[0]);
ASSERT_EQ(3, C[1]);
ASSERT_EQ(4, C[2]);
ASSERT_EQ(5, C[3]);
}
TEST(palignrc, test12) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4];
(palignr)(C, B, A, 12);
ASSERT_EQ(4, C[0]);
ASSERT_EQ(5, C[1]);
ASSERT_EQ(6, C[2]);
ASSERT_EQ(7, C[3]);
}
TEST(palignrc, test16) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4];
(palignr)(C, B, A, 16);
ASSERT_EQ(5, C[0]);
ASSERT_EQ(6, C[1]);
ASSERT_EQ(7, C[2]);
ASSERT_EQ(8, C[3]);
}
TEST(palignrc, test20) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4] = {-1, -1, -1, -1};
(palignr)(C, B, A, 20);
ASSERT_EQ(6, C[0]);
ASSERT_EQ(7, C[1]);
ASSERT_EQ(8, C[2]);
ASSERT_EQ(0, C[3]);
}
TEST(palignr, test32orHigher_clearsOutput) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4] = {-1, -1, -1, -1};
palignr(C, B, A, 32);
EXPECT_EQ(0, C[0]);
EXPECT_EQ(0, C[1]);
EXPECT_EQ(0, C[2]);
EXPECT_EQ(0, C[3]);
ASSERT_EQ(0, C[0]);
ASSERT_EQ(0, C[1]);
ASSERT_EQ(0, C[2]);
ASSERT_EQ(0, C[3]);
C[0] = 43;
palignr(C, B, A, 123);
ASSERT_EQ(0, C[0]);
ASSERT_EQ(0, C[1]);
ASSERT_EQ(0, C[2]);
ASSERT_EQ(0, C[3]);
}
TEST(palignrv, test32orHigher_clearsOutput) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4] = {-1, -1, -1, -1};
palignr(C, B, A, VEIL("r", 32));
ASSERT_EQ(0, C[0]);
ASSERT_EQ(0, C[1]);
ASSERT_EQ(0, C[2]);
ASSERT_EQ(0, C[3]);
C[0] = 43;
palignr(C, B, A, VEIL("r", 123));
ASSERT_EQ(0, C[0]);
ASSERT_EQ(0, C[1]);
ASSERT_EQ(0, C[2]);
ASSERT_EQ(0, C[3]);
}
TEST(palignrc, test32orHigher_clearsOutput) {
const int A[4] = {1, 2, 3, 4};
const int B[4] = {5, 6, 7, 8};
int C[4] = {-1, -1, -1, -1};
(palignr)(C, B, A, 32);
ASSERT_EQ(0, C[0]);
ASSERT_EQ(0, C[1]);
ASSERT_EQ(0, C[2]);
ASSERT_EQ(0, C[3]);
}
BENCH(palignr, bench) {
volatile __intrin_xmm_t A;
volatile __intrin_xmm_t B;
EZBENCH2("palignr const 𝑖", donothing, PROGN(palignr(&A, &A, &B, 7)));
EZBENCH2("palignr var 𝑖", donothing,
PROGN(palignr(&A, &A, &B, VEIL("r", 7))));
EZBENCH2("palignr ansi", donothing, PROGN((palignr)(&A, &A, &B, 7)));
}

View file

@ -1,48 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/intrin/phaddw.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
#include "tool/viz/lib/formatstringtable-testlib.h"
/* clang-format off */
FIXTURE(phaddw, disableHardwareExtensions) {
memset((/*unconst*/ void *)kCpuids, 0, sizeof(kCpuids));
}
TEST(phaddw, testOverflow_wrapsAround) {
short M[2][8] = {
{0x7fff, 0, 0x7fff, 1, 13004, -30425, 20777, -16389},
{-28040, 13318, -1336, -24798, -13876, 3599, -7346, -23575},
};
phaddw(M[0], M[0], M[1]);
EXPECT_SHRTMATRIXEQ(2, 8, M, "\n\
32767 -32768 -17421 4388 -14722 -26134 -10277 -30921\n\
-28040 13318 -1336 -24798 -13876 3599 -7346 -23575");
}
TEST(phaddw, testAliasing_isOk) {
short M[1][8] = {
{0,1, 2,3, 4,5, 6,7},
};
phaddw(M[0],M[0],M[0]);
EXPECT_SHRTMATRIXEQ(1, 8, M, "\n\
1 5 9 13 1 5 9 13");
}

View file

@ -0,0 +1,162 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/intrin/pshufd.h"
#include "libc/intrin/pshufhw.h"
#include "libc/intrin/pshuflw.h"
#include "libc/rand/rand.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
#define T(f, m) \
f(a, x, m); \
(f)(b, x, m); \
EXPECT_EQ(0, memcmp(a, b, 16))
TEST(pshuflw, test) {
int i, j;
int16_t x[8], a[8], b[8];
for (i = 0; i < 10; ++i) {
for (j = 0; j < 8; ++j) x[j] = rand();
T(pshuflw, 0b00000000);
T(pshuflw, 0b00000001);
T(pshuflw, 0b00000011);
T(pshuflw, 0b00001100);
T(pshuflw, 0b00001101);
T(pshuflw, 0b00001111);
T(pshuflw, 0b00110000);
T(pshuflw, 0b00110001);
T(pshuflw, 0b00110011);
T(pshuflw, 0b00111100);
T(pshuflw, 0b00111101);
T(pshuflw, 0b00111111);
T(pshuflw, 0b01000000);
T(pshuflw, 0b01000001);
T(pshuflw, 0b01000011);
T(pshuflw, 0b01001100);
T(pshuflw, 0b01001101);
T(pshuflw, 0b01001111);
T(pshuflw, 0b01110000);
T(pshuflw, 0b01110001);
T(pshuflw, 0b01110011);
T(pshuflw, 0b01111100);
T(pshuflw, 0b01111101);
T(pshuflw, 0b01111111);
T(pshuflw, 0b11000000);
T(pshuflw, 0b11000001);
T(pshuflw, 0b11000011);
T(pshuflw, 0b11001100);
T(pshuflw, 0b11001101);
T(pshuflw, 0b11001111);
T(pshuflw, 0b11110000);
T(pshuflw, 0b11110001);
T(pshuflw, 0b11110011);
T(pshuflw, 0b11111100);
T(pshuflw, 0b11111101);
T(pshuflw, 0b11111111);
}
}
TEST(pshufhw, test) {
int i, j;
int16_t x[8], a[8], b[8];
for (i = 0; i < 10; ++i) {
for (j = 0; j < 8; ++j) x[j] = rand();
T(pshufhw, 0b00000000);
T(pshufhw, 0b00000001);
T(pshufhw, 0b00000011);
T(pshufhw, 0b00001100);
T(pshufhw, 0b00001101);
T(pshufhw, 0b00001111);
T(pshufhw, 0b00110000);
T(pshufhw, 0b00110001);
T(pshufhw, 0b00110011);
T(pshufhw, 0b00111100);
T(pshufhw, 0b00111101);
T(pshufhw, 0b00111111);
T(pshufhw, 0b01000000);
T(pshufhw, 0b01000001);
T(pshufhw, 0b01000011);
T(pshufhw, 0b01001100);
T(pshufhw, 0b01001101);
T(pshufhw, 0b01001111);
T(pshufhw, 0b01110000);
T(pshufhw, 0b01110001);
T(pshufhw, 0b01110011);
T(pshufhw, 0b01111100);
T(pshufhw, 0b01111101);
T(pshufhw, 0b01111111);
T(pshufhw, 0b11000000);
T(pshufhw, 0b11000001);
T(pshufhw, 0b11000011);
T(pshufhw, 0b11001100);
T(pshufhw, 0b11001101);
T(pshufhw, 0b11001111);
T(pshufhw, 0b11110000);
T(pshufhw, 0b11110001);
T(pshufhw, 0b11110011);
T(pshufhw, 0b11111100);
T(pshufhw, 0b11111101);
T(pshufhw, 0b11111111);
}
}
TEST(pshufd, test) {
int i, j;
int32_t x[4], a[4], b[4];
for (i = 0; i < 10; ++i) {
for (j = 0; j < 4; ++j) x[j] = rand();
T(pshufd, 0b00000000);
T(pshufd, 0b00000001);
T(pshufd, 0b00000011);
T(pshufd, 0b00001100);
T(pshufd, 0b00001101);
T(pshufd, 0b00001111);
T(pshufd, 0b00110000);
T(pshufd, 0b00110001);
T(pshufd, 0b00110011);
T(pshufd, 0b00111100);
T(pshufd, 0b00111101);
T(pshufd, 0b00111111);
T(pshufd, 0b01000000);
T(pshufd, 0b01000001);
T(pshufd, 0b01000011);
T(pshufd, 0b01001100);
T(pshufd, 0b01001101);
T(pshufd, 0b01001111);
T(pshufd, 0b01110000);
T(pshufd, 0b01110001);
T(pshufd, 0b01110011);
T(pshufd, 0b01111100);
T(pshufd, 0b01111101);
T(pshufd, 0b01111111);
T(pshufd, 0b11000000);
T(pshufd, 0b11000001);
T(pshufd, 0b11000011);
T(pshufd, 0b11001100);
T(pshufd, 0b11001101);
T(pshufd, 0b11001111);
T(pshufd, 0b11110000);
T(pshufd, 0b11110001);
T(pshufd, 0b11110011);
T(pshufd, 0b11111100);
T(pshufd, 0b11111101);
T(pshufd, 0b11111111);
}
}

View file

@ -5,12 +5,14 @@ PKGS += TEST_LIBC_INTRIN
TEST_LIBC_INTRIN_SRCS := $(wildcard test/libc/intrin/*.c)
TEST_LIBC_INTRIN_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_INTRIN_SRCS))
TEST_LIBC_INTRIN_COMS = $(TEST_LIBC_INTRIN_OBJS:%.o=%.com)
TEST_LIBC_INTRIN_OBJS = \
$(TEST_LIBC_INTRIN_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_INTRIN_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_INTRIN_COMS = \
$(TEST_LIBC_INTRIN_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_INTRIN_BINS = \
$(TEST_LIBC_INTRIN_COMS) \
$(TEST_LIBC_INTRIN_COMS:%=%.dbg)
@ -22,12 +24,17 @@ TEST_LIBC_INTRIN_CHECKS = \
$(TEST_LIBC_INTRIN_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
TEST_LIBC_INTRIN_DIRECTDEPS = \
LIBC_FMT \
LIBC_INTRIN \
LIBC_NEXGEN32E \
LIBC_RAND \
LIBC_LOG \
LIBC_STUBS \
LIBC_TESTLIB \
LIBC_TINYMATH \
TOOL_VIZ_LIB \
LIBC_TESTLIB
LIBC_RUNTIME \
LIBC_X \
TOOL_VIZ_LIB
TEST_LIBC_INTRIN_DEPS := \
$(call uniq,$(foreach x,$(TEST_LIBC_INTRIN_DIRECTDEPS),$($(x))))

View file

@ -0,0 +1,29 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/math.h"
#include "libc/testlib/testlib.h"
TEST(ilogb, test) {
EXPECT_EQ(0x7fffffff, ilogb(INFINITY));
EXPECT_EQ(0, ilogb(1));
EXPECT_EQ(1, ilogb(2));
EXPECT_EQ(2, ilogb(4));
EXPECT_EQ(63, ilogb(1e19));
}

View file

@ -5,12 +5,14 @@ PKGS += TEST_LIBC_MATH
TEST_LIBC_MATH_SRCS := $(wildcard test/libc/math/*.c)
TEST_LIBC_MATH_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_MATH_SRCS))
TEST_LIBC_MATH_COMS = $(TEST_LIBC_MATH_OBJS:%.o=%.com)
TEST_LIBC_MATH_OBJS = \
$(TEST_LIBC_MATH_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_MATH_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_MATH_COMS = \
$(TEST_LIBC_MATH_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_MATH_BINS = \
$(TEST_LIBC_MATH_COMS) \
$(TEST_LIBC_MATH_COMS:%=%.dbg)
@ -23,7 +25,7 @@ TEST_LIBC_MATH_CHECKS = \
TEST_LIBC_MATH_DIRECTDEPS = \
LIBC_CALLS_HEFTY \
LIBC_FMT \
LIBC_TINYMATH \
LIBC_MATH \
LIBC_MEM \
LIBC_RUNTIME \
LIBC_STUBS \

View file

@ -18,7 +18,59 @@
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/macros.h"
#include "libc/mem/mem.h"
#include "libc/rand/rand.h"
#include "libc/runtime/memtrack.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "libc/testlib/testlib.h"
TEST(malloc, test) { free(malloc(123)); }
#define N 1024
#define M 20
TEST(malloc, test) {
static struct stat st;
static volatile int i, j, k, *A[4096], fds[M], *maps[M], mapsizes[M];
memset(fds, -1, sizeof(fds));
memset(maps, -1, sizeof(maps));
for (i = 0; i < N * M; ++i) {
j = rand() % ARRAYLEN(A);
if (A[j]) {
ASSERT_EQ(j, A[j][0]);
A[j] = realloc(A[j], max(sizeof(int), rand() % N));
ASSERT_NE(NULL, A[j]);
ASSERT_EQ(j, A[j][0]);
free(A[j]);
A[j] = NULL;
} else {
A[j] = malloc(max(sizeof(int), rand() % N));
ASSERT_NE(NULL, A[j]);
A[j][0] = j;
}
if (i % M == 0) {
k = rand() % M;
if (fds[k] == -1) {
ASSERT_NE(-1, (fds[k] = open(program_invocation_name, O_RDONLY)));
ASSERT_NE(-1, fstat(fds[k], &st));
ASSERT_NE(MAP_FAILED,
(maps[k] = mmap(NULL, (mapsizes[k] = st.st_size), PROT_READ,
MAP_SHARED, fds[k], 0)));
} else {
ASSERT_NE(-1, munmap(maps[k], mapsizes[k]));
ASSERT_NE(-1, close(fds[k]));
fds[k] = -1;
}
}
}
for (i = 0; i < ARRAYLEN(A); ++i) free(A[i]);
for (i = 0; i < ARRAYLEN(maps); ++i) munmap(maps[i], mapsizes[i]);
for (i = 0; i < ARRAYLEN(fds); ++i) close(fds[i]);
}

View file

@ -5,12 +5,14 @@ PKGS += TEST_LIBC_MEM
TEST_LIBC_MEM_SRCS := $(wildcard test/libc/mem/*.c)
TEST_LIBC_MEM_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_MEM_SRCS))
TEST_LIBC_MEM_COMS = $(TEST_LIBC_MEM_OBJS:%.o=%.com)
TEST_LIBC_MEM_OBJS = \
$(TEST_LIBC_MEM_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_MEM_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_MEM_COMS = \
$(TEST_LIBC_MEM_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_MEM_BINS = \
$(TEST_LIBC_MEM_COMS) \
$(TEST_LIBC_MEM_COMS:%=%.dbg)
@ -22,7 +24,14 @@ TEST_LIBC_MEM_CHECKS = \
TEST_LIBC_MEM_DIRECTDEPS = \
LIBC_MEM \
LIBC_CALLS \
LIBC_STUBS \
LIBC_NEXGEN32E \
LIBC_SYSV \
LIBC_FMT \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_RAND \
LIBC_TESTLIB
TEST_LIBC_MEM_DEPS := \

View file

@ -18,25 +18,40 @@
02110-1301 USA
*/
#include "libc/macros.h"
#include "libc/nexgen32e/crc32.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/str/knuthmultiplicativehash.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/hyperion.h"
#include "libc/testlib/testlib.h"
uint32_t crc32(uint32_t, const void *, int);
uint32_t crc32$pclmul(uint32_t, const void *, size_t);
uint32_t crc32$pclmul2(uint32_t, const void *, size_t);
#include "third_party/zlib/zlib.h"
TEST(crc32, testBigText) {
size_t size;
void *hyperion;
size = kHyperionSize;
EXPECT_EQ(0xe9ded8e6, crc32(0, kHyperion, size));
EXPECT_EQ(0xe9ded8e6, crc32_z(0, kHyperion, size));
hyperion = kHyperion;
EXPECT_EQ(0xe9ded8e6, crc32(0, hyperion, size));
EXPECT_EQ(0xe9ded8e6, crc32_z(0, hyperion, size));
if (X86_HAVE(PCLMUL)) {
size = ROUNDDOWN(size, 64);
EXPECT_EQ(0xc7adc04f, crc32(0, kHyperion, size));
EXPECT_EQ(0xc7adc04f, crc32_z(0, kHyperion, size));
EXPECT_EQ(0xc7adc04f, crc32(0, hyperion, size));
EXPECT_EQ(0xc7adc04f, crc32_z(0, hyperion, size));
EXPECT_EQ(0xc7adc04f,
0xffffffffu ^ crc32$pclmul(0 ^ 0xffffffffu, kHyperion, size));
0xffffffffu ^ crc32$pclmul(0 ^ 0xffffffffu, hyperion, size));
}
}
#define TESTSTR "libc/calls/typedef/sighandler_t.h"
BENCH(crc32c, bench) {
EZBENCH2("crc32c", donothing,
EXPROPRIATE(crc32c(0, VEIL("r", TESTSTR), sizeof(TESTSTR) - 1)));
}
BENCH(KnuthMultiplicativeHash32, bench) {
EZBENCH2("KMP", donothing,
EXPROPRIATE(KnuthMultiplicativeHash32(VEIL("r", TESTSTR),
sizeof(TESTSTR) - 1)));
}

View file

@ -17,19 +17,14 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/bits/bits.h"
#include "libc/macros.h"
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
#include "libc/unicode/unicode.h"
TEST(heapsortcar, test) {
int32_t A[][2] = {{4, 'a'}, {65, 'b'}, {2, 'c'}, {-31, 'd'}, {0, 'e'},
{99, 'f'}, {2, 'g'}, {83, 'h'}, {782, 'i'}, {1, 'j'}};
const int32_t B[][2] = {{-31, 'd'}, {0, 'e'}, {1, 'j'}, {2, 'c'},
{2, 'g'}, {4, 'a'}, {65, 'b'}, {83, 'h'},
{99, 'f'}, {782, 'i'}};
unsigned n = ARRAYLEN(A);
heapsortcar(A, n);
ASSERT_EQ(0, memcmp(&A[0], &B[0], sizeof(A)));
TEST(kcp437, test) {
long i;
for (i = 0; i < 256; ++i) {
EXPECT_EQ(1, wcwidth(kCp437[i]), "%lc", kCp437[i]);
}
}

View file

@ -20,7 +20,6 @@
#include "libc/bits/bits.h"
#include "libc/runtime/buffer.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/mappings.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"

View file

@ -25,12 +25,15 @@
#define N 256
#define S 7
long i, j, n;
char *b1, *b2;
TEST(memmove, overlapping) {
for (size_t i = 0; i < N; i += S) {
for (size_t j = 10; j < N; j += S) {
char *b1 = tmalloc(N);
char *b2 = tmalloc(N);
size_t n = min(N - i, N - j);
for (i = 0; i < N; i += S) {
for (j = 10; j < N; j += S) {
b1 = tmalloc(N);
b2 = tmalloc(N);
n = min(N - i, N - j);
memcpy(b2, b1 + i, n);
ASSERT_EQ(b1 + j, memmove(b1 + j, b1 + i, n));
ASSERT_EQ(0, memcmp(b1 + j, b2, n));
@ -41,11 +44,11 @@ TEST(memmove, overlapping) {
}
TEST(memmove, overlappingDirect) {
for (size_t i = 0; i < N; i += S) {
for (size_t j = 10; j < N; j += S) {
char *b1 = tmalloc(N);
char *b2 = tmalloc(N);
size_t n = min(N - i, N - j);
for (i = 0; i < N; i += S) {
for (j = 10; j < N; j += S) {
b1 = tmalloc(N);
b2 = tmalloc(N);
n = min(N - i, N - j);
memcpy(b2, b1 + i, n);
ASSERT_EQ(b1 + j, (memmove)(b1 + j, b1 + i, n));
ASSERT_EQ(0, memcmp(b1 + j, b2, n));

View file

@ -17,54 +17,55 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/intrin/packsswb.h"
#include "libc/intrin/packuswb.h"
#include "libc/limits.h"
#include "libc/nexgen32e/kcpuids.h"
#include "libc/str/str.h"
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/testlib/testlib.h"
const short S[8] = {0, 128, -128, 255, SHRT_MAX, SHRT_MIN, 0, 0};
TEST(packuswb, test) {
unsigned char B[16] = {0};
packuswb(B, S, S);
EXPECT_EQ(0, B[0]);
EXPECT_EQ(128, B[1]);
EXPECT_EQ(0, B[2]);
EXPECT_EQ(255, B[3]);
EXPECT_EQ(255, B[4]);
EXPECT_EQ(0, B[5]);
EXPECT_EQ(0, B[6]);
EXPECT_EQ(0, B[7]);
EXPECT_EQ(0, B[8]);
EXPECT_EQ(128, B[9]);
EXPECT_EQ(0, B[10]);
EXPECT_EQ(255, B[11]);
EXPECT_EQ(255, B[12]);
EXPECT_EQ(0, B[13]);
EXPECT_EQ(0, B[14]);
EXPECT_EQ(0, B[15]);
TEST(memset, testMulTrick4) {
long i, j;
unsigned long x;
long di, si, dx, ax;
volatile uint8_t *b;
b = tgc(tmalloc(4));
for (i = 0; i < 255; ++i) {
for (j = -1; j < 1; ++j) {
x = j;
x &= ~0xff;
x |= i;
asm volatile("call\tmemset"
: "=D"(di), "=S"(si), "=d"(dx), "=a"(ax)
: "0"(b), "1"(x), "2"(4)
: "rcx", "memory", "cc");
ASSERT_EQ(x & 0xff, b[0]);
ASSERT_EQ(x & 0xff, b[1]);
ASSERT_EQ(x & 0xff, b[2]);
ASSERT_EQ(x & 0xff, b[3]);
}
}
}
TEST(packsswb, test) {
const short S[8] = {0, 128, -128, 255, SHRT_MAX, SHRT_MIN, 0, 0};
signed char B[16] = {0};
packsswb(B, S, S);
EXPECT_EQ(0, B[0]);
EXPECT_EQ(127, B[1]);
EXPECT_EQ(-128, B[2]);
EXPECT_EQ(127, B[3]);
EXPECT_EQ(127, B[4]);
EXPECT_EQ(-128, B[5]);
EXPECT_EQ(0, B[6]);
EXPECT_EQ(0, B[7]);
EXPECT_EQ(0, B[8]);
EXPECT_EQ(127, B[9]);
EXPECT_EQ(-128, B[10]);
EXPECT_EQ(127, B[11]);
EXPECT_EQ(127, B[12]);
EXPECT_EQ(-128, B[13]);
EXPECT_EQ(0, B[14]);
EXPECT_EQ(0, B[15]);
TEST(memset, testMulTrick8) {
long i, j;
unsigned long x;
long di, si, dx, ax;
volatile uint8_t *b;
b = tgc(tmalloc(8));
for (i = 0; i < 255; ++i) {
for (j = -1; j < 1; ++j) {
x = j;
x &= ~0xff;
x |= i;
asm volatile("call\tmemset"
: "=D"(di), "=S"(si), "=d"(dx), "=a"(ax)
: "0"(b), "1"(x), "2"(8)
: "rcx", "memory", "cc");
ASSERT_EQ(x & 0xff, b[0]);
ASSERT_EQ(x & 0xff, b[1]);
ASSERT_EQ(x & 0xff, b[2]);
ASSERT_EQ(x & 0xff, b[3]);
ASSERT_EQ(x & 0xff, b[4]);
ASSERT_EQ(x & 0xff, b[5]);
ASSERT_EQ(x & 0xff, b[6]);
ASSERT_EQ(x & 0xff, b[7]);
}
}
}

View file

@ -0,0 +1,48 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/testlib/testlib.h"
TEST(sidiv, smoke) {
EXPECT_EQ(13373133731337 / 10, div10int64(13373133731337));
EXPECT_EQ(13373133731337 / 100, div100int64(13373133731337));
EXPECT_EQ(13373133731337 / 1000, div1000int64(13373133731337));
EXPECT_EQ(13373133731337 / 10000, div10000int64(13373133731337));
EXPECT_EQ(13373133731337 / 1000000, div1000000int64(13373133731337));
EXPECT_EQ(13373133731337 / 1000000000, div1000000000int64(13373133731337));
}
TEST(sirem, smoke) {
EXPECT_EQ(13373133731337 % 10, rem10int64(13373133731337));
EXPECT_EQ(13373133731337 % 100, rem100int64(13373133731337));
EXPECT_EQ(13373133731337 % 1000, rem1000int64(13373133731337));
EXPECT_EQ(13373133731337 % 10000, rem10000int64(13373133731337));
EXPECT_EQ(13373133731337 % 1000000, rem1000000int64(13373133731337));
EXPECT_EQ(13373133731337 % 1000000000, rem1000000000int64(13373133731337));
}
TEST(rem, euclid) {
ASSERT_EQ(-2, rem10int64(-12));
ASSERT_EQ(-1, rem10int64(-1));
ASSERT_EQ(0, rem10int64(0));
ASSERT_EQ(1, rem10int64(1));
ASSERT_EQ(9, rem10int64(9));
ASSERT_EQ(1, rem10int64(11));
}

View file

@ -24,25 +24,31 @@
#include "libc/testlib/testlib.h"
TEST(strtolower, testAligned) {
char s[128] = "AZCDabcdABCDabcdABCDabcdABCDabcdABCDabcd";
EXPECT_STREQ("azcdabcdabcdabcdabcdabcdabcdabcdabcdabcd", strtolower(s));
EXPECT_STREQ("azcdabcdabcdabcd",
strtolower(tgc(tstrdup("AZCDabcdABCDabcd"))));
EXPECT_STREQ("azcdabcdabcdabcdabcdabcdabcdabcd",
strtolower(tgc(tstrdup("AZCDabcdABCDabcdABCDabcdABCDabcd"))));
}
TEST(strtolower, testUnaligned) {
char s[128] = "AZCDabcdABCDabcdABCDabcdABCDabcdABCDabcd";
strtolower(s + 1);
EXPECT_STREQ("Azcdabcdabcdabcdabcdabcdabcdabcdabcdabcd", s);
EXPECT_STREQ("1", strtolower(tgc(tstrdup("1"))));
EXPECT_STREQ(
"zcdabcdabcdabcdabcdabcdabcdabc",
strtolower((char *)tgc(tstrdup("AZCDabcdABCDabcdABCDabcdABCDabc")) + 1));
}
TEST(strtoupper, testAligned) {
char s[128] = "AZCDabcdABCDabcdA0CDabcdABCDabcdABCDabcd";
EXPECT_STREQ("AZCDABCDABCDABCDA0CDABCDABCDABCDABCDABCD", strtoupper(s));
EXPECT_STREQ("AZCDABCDABCDABCD",
strtoupper(tgc(tstrdup("AZCDabcdABCDabcd"))));
EXPECT_STREQ("AZCDABCDABCDABCDABCDABCDABCDABCD",
strtoupper(tgc(tstrdup("AZCDabcdABCDabcdABCDabcdABCDabcd"))));
}
TEST(strtoupper, testUnaligned) {
char s[128] = "aZCDabcdABCDabcdABCDabcdABCDabcdABCDabcd";
strtoupper(s + 1);
EXPECT_STREQ("aZCDABCDABCDABCDABCDABCDABCDABCDABCDABCD", s);
EXPECT_STREQ("1", strtoupper(tgc(tstrdup("1"))));
EXPECT_STREQ(
"ZCDABCDABCDABCDABCDABCDABCDABC",
strtoupper((char *)tgc(tstrdup("AZCDabcdABCDabcdABCDabcdABCDabc")) + 1));
}
BENCH(strtolower, bench) {

View file

@ -13,7 +13,7 @@ TEST_LIBC_NEXGEN32E_OBJS = \
$(TEST_LIBC_NEXGEN32E_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_NEXGEN32E_COMS = \
$(TEST_LIBC_NEXGEN32E_OBJS:%.o=%.com)
$(TEST_LIBC_NEXGEN32E_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_NEXGEN32E_BINS = \
$(TEST_LIBC_NEXGEN32E_COMS) \
@ -38,6 +38,7 @@ TEST_LIBC_NEXGEN32E_DIRECTDEPS = \
LIBC_RUNTIME \
LIBC_STUBS \
LIBC_STR \
LIBC_UNICODE \
LIBC_TESTLIB \
LIBC_X \
TOOL_VIZ_LIB

View file

@ -5,13 +5,15 @@ PKGS += TEST_LIBC_RAND
TEST_LIBC_RAND_SRCS := $(wildcard test/libc/rand/*.c)
TEST_LIBC_RAND_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_RAND_SRCS))
TEST_LIBC_RAND_COMS = $(TEST_LIBC_RAND_OBJS:%.o=%.com)
TEST_LIBC_RAND_BINS = $(TEST_LIBC_RAND_COMS) $(TEST_LIBC_RAND_COMS:%=%.dbg)
TEST_LIBC_RAND_OBJS = \
$(TEST_LIBC_RAND_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_RAND_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_RAND_COMS = \
$(TEST_LIBC_RAND_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_RAND_TESTS = $(TEST_LIBC_RAND_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
TEST_LIBC_RAND_CHECKS = \
@ -45,9 +47,7 @@ o/$(MODE)/test/libc/rand/%.com.dbg: \
$(APE)
@$(APELINK)
$(TEST_LIBC_RAND_OBJS): \
$(BUILD_FILES) \
test/libc/rand/test.mk
$(TEST_LIBC_RAND_OBJS): test/libc/rand/test.mk
$(TEST_LIBC_RAND_OBJS): \
DEFAULT_CCFLAGS += \

View file

@ -17,8 +17,9 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/dce.h"
#include "libc/bits/segmentation.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/testlib/testlib.h"
TEST(arch_prctl, fs) {

View file

@ -1,105 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/safemacros.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/ucontext.h"
#include "libc/runtime/buffer.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/sa.h"
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
#include "third_party/xed/x86.h"
char *p;
bool segfaulted_;
struct GuardedBuffer b_;
struct sigaction oldsegv_;
struct XedDecodedInst xedd_;
void RestrictPage(void *addr, unsigned flags) {
addr = (void *)rounddown((intptr_t)addr, PAGESIZE);
EXPECT_NE(-1, mprotect(addr, PAGESIZE, flags));
}
void OnSegLol(int sig, struct siginfo *si, struct ucontext *uc) {
size_t i;
uint8_t *rip;
segfaulted_ = true;
rip = (uint8_t *)uc->uc_mcontext.rip;
RestrictPage(rip, PROT_READ | PROT_WRITE | PROT_EXEC);
ASSERT_EQ(XED_ERROR_NONE,
xed_instruction_length_decode(xed_decoded_inst_zero_set_mode(
&xedd_, XED_MACHINE_MODE_LONG_64),
rip, XED_MAX_INSTRUCTION_BYTES));
for (i = 0; i < xedd_.decoded_length; ++i) rip[i] = 0x90; /* NOP */
RestrictPage(rip, PROT_READ | PROT_EXEC);
}
void SetUp(void) {
segfaulted_ = false;
memset(&b_, 0, sizeof(b_));
ASSERT_NE(-1, xsigaction(SIGSEGV, OnSegLol, SA_RESETHAND | SA_RESTART, 0,
&oldsegv_));
}
void TearDown(void) {
EXPECT_NE(-1, sigaction(SIGSEGV, &oldsegv_, NULL));
bfree(&b_);
EXPECT_EQ(NULL, b_.p);
}
TEST(balloc, createsGuardPage) {
ASSERT_NE(NULL, (p = balloc(&b_, 1, 1)));
EXPECT_EQ(p, b_.p);
p[0] = '.';
ASSERT_FALSE(segfaulted_);
/* TODO(jart): fix me!!! */
/* p[1 + __BIGGEST_ALIGNMENT__] = '!'; */
/* EXPECT_TRUE(segfaulted_); */
}
TEST(balloc, aligned_roundsUp) {
ASSERT_NE(NULL, (p = balloc(&b_, 128, 1)));
EXPECT_EQ(0, (intptr_t)b_.p & 127);
p[127] = '.';
ASSERT_FALSE(segfaulted_);
/* TODO(jart): fix me!!! */
/* p[128 + __BIGGEST_ALIGNMENT__] = '!'; */
/* EXPECT_TRUE(segfaulted_); */
}
TEST(balloc, multipleCalls_avoidsNeedlessSyscalls) {
size_t c;
c = g_syscount;
ASSERT_NE(NULL, (p = balloc(&b_, 1, 31337)));
EXPECT_GT(g_syscount, c);
c = g_syscount;
ASSERT_NE(NULL, (p = balloc(&b_, 1, 31337 / 2)));
EXPECT_EQ(g_syscount, c);
c = g_syscount;
ASSERT_NE(NULL, (p = balloc(&b_, 1, 31337 * 2)));
EXPECT_GT(g_syscount, c);
}

View file

@ -17,42 +17,59 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/limits.h"
#include "libc/alg/alg.h"
#include "libc/macros.h"
#include "libc/runtime/mappings.h"
#include "libc/rand/rand.h"
#include "libc/runtime/carsort.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#define ADDR
const int32_t kUnsorted[][2] = {
{4, 'a'}, {65, 'b'}, {2, 'c'}, {-1, 'G'}, {-31, 'd'}, {0, 'e'},
{99, 'f'}, {2, 'g'}, {83, 'h'}, {782, 'i'}, {1, 'j'},
};
struct MemoryCoord stack = ADDRSIZE_TO_COORD(0x7fffffff0000L, 0x00010000);
struct MemoryCoord heap3 = ADDRSIZE_TO_COORD(0x200000020000L, 0x00010000);
struct MemoryCoord heap2 = ADDRSIZE_TO_COORD(0x200000010000L, 0x00010000);
struct MemoryCoord heap1 = ADDRSIZE_TO_COORD(0x200000000000L, 0x00010000);
struct MemoryCoord heapa = ADDRSIZE_TO_COORD(0x200000000000L, 0x00030000);
struct MemoryCoord progg = ADDRSIZE_TO_COORD(0x000000400000L, 0x00010000);
struct MemoryCoord bane = ADDRSIZE_TO_COORD(0xffff800000080000L, 0x00010000);
const int32_t kGolden[][2] = {
{-31, 'd'}, {-1, 'G'}, {0, 'e'}, {1, 'j'}, {2, 'c'}, {2, 'g'},
{4, 'a'}, {65, 'b'}, {83, 'h'}, {99, 'f'}, {782, 'i'},
};
TEST(isoverlapping, test) {
EXPECT_FALSE(ISOVERLAPPING(stack, heap3));
EXPECT_FALSE(ISOVERLAPPING(heap1, heap2));
EXPECT_FALSE(ISOVERLAPPING(heap2, heap3));
EXPECT_FALSE(ISOVERLAPPING(heap1, heap3));
EXPECT_TRUE(ISOVERLAPPING(heapa, heap1));
EXPECT_TRUE(ISOVERLAPPING(heapa, heap3));
int32_t A[ARRAYLEN(kUnsorted)][2];
int32_t B[2100][2];
TEST(carsort100, test) {
memcpy(A, kUnsorted, sizeof(A));
carsort100(ARRAYLEN(A), A);
ASSERT_EQ(0, memcmp(&A[0], &kGolden[0], sizeof(kUnsorted)));
}
TEST(findmapping, limits) {
ASSERT_EQ(INT_MAX, ADDR_TO_COORD(0x7fffffff0000L));
ASSERT_EQ(INT_MIN, ADDR_TO_COORD(-0x800000000000L));
TEST(carsort1000, test) {
memcpy(A, kUnsorted, sizeof(A));
carsort1000(ARRAYLEN(A), A);
ASSERT_EQ(0, memcmp(&A[0], &kGolden[0], sizeof(kUnsorted)));
}
TEST(findmapping, test) {
struct MemoryCoord c[] = {bane, progg, heap1, heap2, heap3, stack};
EXPECT_EQ(6, ARRAYLEN(c));
EXPECT_EQ(0, findmapping_(ADDR_TO_COORD(-0x800000000000UL), c, 6));
EXPECT_EQ(1, findmapping_(ADDR_TO_COORD(-42), c, 6));
EXPECT_EQ(1, findmapping_(ADDR_TO_COORD(0x000000300000L), c, 6));
EXPECT_EQ(2, findmapping_(ADDR_TO_COORD(0x000000400000L), c, 6));
EXPECT_EQ(6, findmapping_(ADDR_TO_COORD(0x7fffffffffffL), c, 6));
TEST(qsort, test) {
memcpy(A, kUnsorted, sizeof(A));
qsort(A, ARRAYLEN(A), 8, cmpsl);
ASSERT_EQ(0, memcmp(&A[0], &kGolden[0], sizeof(kUnsorted)));
}
BENCH(carsort, benchMedium) {
EZBENCH2("medium carsort100", rngset(B, sizeof(B), rand64, -1),
carsort100(ARRAYLEN(B), B));
EZBENCH2("medium carsort1000", rngset(B, sizeof(B), rand64, -1),
carsort1000(ARRAYLEN(B), B));
EZBENCH2("medium qsort", rngset(B, sizeof(B), rand64, -1),
qsort(B, ARRAYLEN(B), 8, cmpsl));
}
BENCH(carsort, benchSmall) {
EZBENCH2("small carsort100", memcpy(A, kUnsorted, sizeof(A)),
carsort100(ARRAYLEN(A), A));
EZBENCH2("small carsort1000", memcpy(A, kUnsorted, sizeof(A)),
carsort1000(ARRAYLEN(A), A));
EZBENCH2("small qsort", memcpy(A, kUnsorted, sizeof(A)),
qsort(A, ARRAYLEN(A), 8, cmpsl));
}

View file

@ -21,7 +21,6 @@
#include "libc/limits.h"
#include "libc/macros.h"
#include "libc/mem/mem.h"
#include "libc/runtime/mappings.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"

View file

@ -36,7 +36,7 @@
* @see __addvsi3, __mulvsi3, etc.
*/
bool overflowed_;
volatile bool overflowed_;
void __on_arithmetic_overflow(void) {
overflowed_ = true;
@ -174,6 +174,16 @@ TEST(__mulvdi3, standAndDeliver_aNegativeTimesANegativeEqualsAPositive) {
EXPECT_FALSE(overflowed_);
}
TEST(__mulvdi3, testOverflow) {
volatile int64_t x;
x = 3037000500;
x *= 3037000499;
EXPECT_FALSE(overflowed_);
x = 3037000500;
x *= 3037000500;
EXPECT_TRUE(overflowed_);
}
/* 32-BIT SIGNED ADDITION */
TEST(__addvsi3, testMin1) {
@ -246,3 +256,99 @@ TEST(__subvdi3, testMax1) {
EXPECT_EQ(LONG_MAX - 1, VEIL("r", LONG_MAX) - 1);
EXPECT_FALSE(overflowed_);
}
/* 128-BIT SIGNED ADDITION */
TEST(__addvti3, testMath) {
volatile int128_t x;
x = 2;
x += 2;
EXPECT_EQ(4, x);
x = -2;
x += -2;
EXPECT_EQ(-4, x);
x = UINT64_MAX;
x += 1;
EXPECT_EQ((int128_t)UINT64_MAX + 1, x);
EXPECT_FALSE(overflowed_);
}
TEST(__addvti3, testOverflow) {
volatile int128_t x;
x = INT128_MAX;
x += 1;
EXPECT_TRUE(overflowed_);
}
/* 128-BIT SIGNED SUBTRACTION */
TEST(__subvti3, testMath) {
volatile int128_t x;
x = -2;
x -= 2;
EXPECT_EQ(-4, x);
x = UINT64_MIN;
x -= 1;
EXPECT_EQ((int128_t)UINT64_MIN - 1, x);
EXPECT_FALSE(overflowed_);
}
TEST(__subvti3, testOverflow) {
volatile int128_t x;
x = INT128_MIN;
x -= 1;
EXPECT_TRUE(overflowed_);
}
/* 128-BIT SIGNED NEGATION */
TEST(__negvti3, testMath) {
volatile int128_t x;
x = -2;
x = -x;
EXPECT_EQ(2, x);
EXPECT_FALSE(overflowed_);
x = INT128_MAX;
x = -x;
EXPECT_EQ(INT128_MIN + 1, x);
EXPECT_FALSE(overflowed_);
x = (uint128_t)0x8000000000000000 << 64 | 0x8000000000000000;
x = -x;
EXPECT_EQ((uint128_t)0x7fffffffffffffff << 64 | 0x8000000000000000, x);
EXPECT_FALSE(overflowed_);
}
TEST(__negvti3, testOverflow) {
volatile int128_t x;
x = INT128_MIN;
x = -x;
EXPECT_TRUE(overflowed_);
}
/* 128-BIT SIGNED MULTIPLICATION */
TEST(__mulvti3, testMath) {
volatile int128_t x;
x = 7;
x *= 11;
EXPECT_EQ(77, x);
EXPECT_FALSE(overflowed_);
x = 0x1fffffffffffffff;
x *= 0x1fffffffffffffff;
EXPECT_EQ((uint128_t)0x3ffffffffffffff << 64 | 0xc000000000000001, x);
EXPECT_FALSE(overflowed_);
x = -0x1fffffffffffffff;
x *= 0x1fffffffffffffff;
EXPECT_EQ((uint128_t)0xfc00000000000000 << 64 | 0x3fffffffffffffff, x);
EXPECT_FALSE(overflowed_);
}
TEST(__mulvti3, testOverflow) {
volatile int128_t x;
x = 0xb504f333f9de5be0;
x *= 0xb504f333f9de6d28;
EXPECT_FALSE(overflowed_);
x = 0xb504f333f9de5be0;
x *= 0xb504f333f9de6d29;
EXPECT_TRUE(overflowed_);
}

View file

@ -0,0 +1,289 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/errno.h"
#include "libc/limits.h"
#include "libc/log/check.h"
#include "libc/mem/mem.h"
#include "libc/runtime/memtrack.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
static bool AreMemoryIntervalsEqual(const struct MemoryIntervals *mm1,
const struct MemoryIntervals *mm2) {
if (mm1->i != mm2->i) return false;
if (memcmp(mm1->p, mm2->p, mm1->i * sizeof(*mm2->p)) != 0) return false;
if (memcmp(mm1->h, mm2->h, mm1->i * sizeof(*mm2->h)) != 0) return false;
return true;
}
static void PrintMemoryInterval(const struct MemoryIntervals *mm) {
int i;
for (i = 0; i < mm->i; ++i) {
if (i) fprintf(stderr, ",");
fprintf(stderr, "{%d,%d}", mm->p[i].x, mm->p[i].y);
}
fprintf(stderr, "\n");
}
static void CheckMemoryIntervalsEqual(const struct MemoryIntervals *mm1,
const struct MemoryIntervals *mm2) {
if (!AreMemoryIntervalsEqual(mm1, mm2)) {
PrintMemoryInterval(mm1);
PrintMemoryInterval(mm2);
CHECK(!"memory intervals not equal");
exit(1);
}
}
static void CheckMemoryIntervalsAreOk(const struct MemoryIntervals *mm) {
if (!AreMemoryIntervalsOk(mm)) {
PrintMemoryInterval(mm);
CHECK(!"memory intervals not ok");
exit(1);
}
}
static void RunTrackMemoryIntervalTest(const struct MemoryIntervals t[2], int x,
int y, long h) {
struct MemoryIntervals *mm;
mm = memcpy(memalign(alignof(*t), sizeof(*t)), t, sizeof(*t));
CheckMemoryIntervalsAreOk(mm);
CHECK_NE(-1, TrackMemoryInterval(mm, x, y, h));
CheckMemoryIntervalsAreOk(mm);
CheckMemoryIntervalsEqual(mm, t + 1);
free(mm);
}
static void RunReleaseMemoryIntervalsTest(const struct MemoryIntervals t[2],
int x, int y) {
struct MemoryIntervals *mm;
mm = memcpy(memalign(alignof(*t), sizeof(*t)), t, sizeof(*t));
CheckMemoryIntervalsAreOk(mm);
CHECK_NE(-1, ReleaseMemoryIntervals(mm, x, y, NULL));
CheckMemoryIntervalsAreOk(mm);
CheckMemoryIntervalsEqual(t + 1, mm);
free(mm);
}
TEST(TrackMemoryInterval, TestEmpty) {
static const struct MemoryIntervals mm[2] = {
{0, {}, {}},
{1, {{2, 2}}, {0}},
};
RunTrackMemoryIntervalTest(mm, 2, 2, 0);
}
TEST(TrackMemoryInterval, TestFull) {
int i;
struct MemoryIntervals *mm;
mm = calloc(1, sizeof(struct MemoryIntervals));
for (i = 0; i < ARRAYLEN(mm->p); ++i) {
CheckMemoryIntervalsAreOk(mm);
CHECK_NE(-1, TrackMemoryInterval(mm, i, i, i));
CheckMemoryIntervalsAreOk(mm);
}
CHECK_EQ(-1, TrackMemoryInterval(mm, i, i, i));
CHECK_EQ(ENOMEM, errno);
CheckMemoryIntervalsAreOk(mm);
free(mm);
}
TEST(TrackMemoryInterval, TestAppend) {
static const struct MemoryIntervals mm[2] = {
{1, {{2, 2}}, {0}},
{1, {{2, 3}}, {0}},
};
RunTrackMemoryIntervalTest(mm, 3, 3, 0);
}
TEST(TrackMemoryInterval, TestPrepend) {
static const struct MemoryIntervals mm[2] = {
{1, {{2, 2}}, {0}},
{1, {{1, 2}}, {0}},
};
RunTrackMemoryIntervalTest(mm, 1, 1, 0);
}
TEST(TrackMemoryInterval, TestFillHole) {
static const struct MemoryIntervals mm[2] = {
{4, {{1, 1}, {3, 4}, {5, 5}, {6, 8}}, {0, 0, 1, 0}},
{3, {{1, 4}, {5, 5}, {6, 8}}, {0, 1, 0}},
};
RunTrackMemoryIntervalTest(mm, 2, 2, 0);
}
TEST(TrackMemoryInterval, TestAppend2) {
static const struct MemoryIntervals mm[2] = {
{1, {{2, 2}}, {0}},
{2, {{2, 2}, {3, 3}}, {0, 1}},
};
RunTrackMemoryIntervalTest(mm, 3, 3, 1);
}
TEST(TrackMemoryInterval, TestPrepend2) {
static const struct MemoryIntervals mm[2] = {
{1, {{2, 2}}, {0}},
{2, {{1, 1}, {2, 2}}, {1, 0}},
};
RunTrackMemoryIntervalTest(mm, 1, 1, 1);
}
TEST(TrackMemoryInterval, TestFillHole2) {
static const struct MemoryIntervals mm[2] = {
{4, {{1, 1}, {3, 4}, {5, 5}, {6, 8}}, {0, 0, 1, 0}},
{5, {{1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 8}}, {0, 1, 0, 1, 0}},
};
RunTrackMemoryIntervalTest(mm, 2, 2, 1);
}
TEST(FindMemoryInterval, Test) {
static const struct MemoryIntervals mm[1] = {
{4,
{
[0] = {1, 1},
[1] = {3, 4},
[2] = {5, 5},
[3] = {6, 8},
},
{0, 0, 1, 0}},
};
EXPECT_EQ(0, FindMemoryInterval(mm, 0));
EXPECT_EQ(0, FindMemoryInterval(mm, 1));
EXPECT_EQ(1, FindMemoryInterval(mm, 2));
EXPECT_EQ(1, FindMemoryInterval(mm, 3));
EXPECT_EQ(1, FindMemoryInterval(mm, 4));
EXPECT_EQ(2, FindMemoryInterval(mm, 5));
EXPECT_EQ(3, FindMemoryInterval(mm, 6));
EXPECT_EQ(3, FindMemoryInterval(mm, 7));
EXPECT_EQ(3, FindMemoryInterval(mm, 8));
EXPECT_EQ(4, FindMemoryInterval(mm, 9));
}
TEST(ReleaseMemoryIntervals, TestEmpty) {
static const struct MemoryIntervals mm[2] = {
{0, {}, {}},
{0, {}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 2, 2);
}
TEST(ReleaseMemoryIntervals, TestRemoveElement_UsesInclusiveRange) {
static const struct MemoryIntervals mm[2] = {
{3, {{0, 0}, {2, 2}, {4, 4}}, {}},
{2, {{0, 0}, {4, 4}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 2, 2);
}
TEST(ReleaseMemoryIntervals, TestPunchHole) {
static const struct MemoryIntervals mm[2] = {
{1, {{0, 9}}, {}},
{2, {{0, 3}, {6, 9}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 4, 5);
}
TEST(ReleaseMemoryIntervals, TestShortenLeft) {
static const struct MemoryIntervals mm[2] = {
{1, {{0, 9}}, {}},
{1, {{0, 7}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 8, 9);
}
TEST(ReleaseMemoryIntervals, TestShortenRight) {
static const struct MemoryIntervals mm[2] = {
{1, {{0, 9}}, {}},
{1, {{3, 9}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 0, 2);
}
TEST(ReleaseMemoryIntervals, TestShortenLeft2) {
static const struct MemoryIntervals mm[2] = {
{1, {{0, 9}}, {}},
{1, {{0, 7}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 8, 11);
}
TEST(ReleaseMemoryIntervals, TestShortenRight2) {
static const struct MemoryIntervals mm[2] = {
{1, {{0, 9}}, {}},
{1, {{3, 9}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, -3, 2);
}
TEST(ReleaseMemoryIntervals, TestZeroZero) {
static const struct MemoryIntervals mm[2] = {
{1, {{3, 9}}, {}},
{1, {{3, 9}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 0, 0);
}
TEST(ReleaseMemoryIntervals, TestNoopLeft) {
static const struct MemoryIntervals mm[2] = {
{1, {{3, 9}}, {}},
{1, {{3, 9}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 1, 2);
}
TEST(ReleaseMemoryIntervals, TestNoopRight) {
static const struct MemoryIntervals mm[2] = {
{1, {{3, 9}}, {}},
{1, {{3, 9}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 10, 10);
}
TEST(ReleaseMemoryIntervals, TestBigFree) {
static const struct MemoryIntervals mm[2] = {
{2, {{0, 3}, {6, 9}}, {}},
{0, {}, {}},
};
RunReleaseMemoryIntervalsTest(mm, INT_MIN, INT_MAX);
}
TEST(ReleaseMemoryIntervals, TestWeirdGap) {
static const struct MemoryIntervals mm[2] = {
{3, {{10, 10}, {20, 20}, {30, 30}}, {}},
{2, {{10, 10}, {30, 30}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 15, 25);
}
TEST(ReleaseMemoryIntervals, TestOutOfMemory) {
int i;
struct MemoryIntervals *mm;
mm = calloc(1, sizeof(struct MemoryIntervals));
for (i = 0; i < ARRAYLEN(mm->p); ++i) {
CHECK_NE(-1, TrackMemoryInterval(mm, i * 10, i * 10 + 8, 0));
}
CheckMemoryIntervalsAreOk(mm);
CHECK_EQ(-1, ReleaseMemoryIntervals(mm, 4, 4, NULL));
CHECK_EQ(ENOMEM, errno);
CheckMemoryIntervalsAreOk(mm);
free(mm);
}

View file

@ -21,8 +21,9 @@
#include "libc/bits/xchg.h"
#include "libc/calls/calls.h"
#include "libc/fmt/fmt.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/mappings.h"
#include "libc/runtime/memtrack.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
@ -33,71 +34,17 @@
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
unsigned m1;
TEST(mmap, testMapUnmapAnonAnyAddr) {
void *p;
m1 = _mm.i;
EXPECT_NE(MAP_FAILED, (p = mmap(NULL, FRAMESIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)));
EXPECT_EQ(m1 + 1, _mm.i);
EXPECT_NE(-1, munmap(p, FRAMESIZE));
EXPECT_EQ(m1 + 0, _mm.i);
}
TEST(mmap, testMunmapUnmapsMultiple) {
void *p1, *p2;
m1 = _mm.i;
EXPECT_NE(MAP_FAILED, (p1 = mmap(NULL, FRAMESIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)));
EXPECT_NE(MAP_FAILED, (p2 = mmap(NULL, FRAMESIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)));
if ((intptr_t)p1 > (intptr_t)p2) xchg(&p1, &p2);
EXPECT_EQ(m1 + 2, _mm.i);
EXPECT_NE(-1, munmap(p1, (intptr_t)p2 + (intptr_t)FRAMESIZE - (intptr_t)p1));
EXPECT_EQ(m1 + 0, _mm.i);
}
TEST(mmap, testPartialUnmapRight) {
if (1) return; /* naaah */
char *p;
m1 = _mm.i;
EXPECT_NE(MAP_FAILED, (p = mmap(NULL, FRAMESIZE * 2, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)));
EXPECT_EQ(m1 + 1, _mm.i);
EXPECT_NE(-1, munmap(p + FRAMESIZE, FRAMESIZE));
EXPECT_EQ(m1 + 1, _mm.i);
EXPECT_NE(-1, munmap(p, FRAMESIZE));
EXPECT_EQ(m1 + 0, _mm.i);
}
TEST(mmap, testPartialUnmapLeft) {
if (1) return; /* naaah */
char *p;
m1 = _mm.i;
EXPECT_NE(MAP_FAILED, (p = mmap(NULL, FRAMESIZE * 2, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)));
EXPECT_EQ(m1 + 1, _mm.i);
EXPECT_NE(-1, munmap(p, FRAMESIZE));
EXPECT_EQ(m1 + 1, _mm.i);
EXPECT_NE(-1, munmap(p + FRAMESIZE, FRAMESIZE));
EXPECT_EQ(m1 + 0, _mm.i);
}
TEST(mmap, testMapFile) {
int fd;
char *p;
char path[PATH_MAX];
sprintf(path, "%s%s.%d", kTmpPath, program_invocation_short_name, getpid());
m1 = _mm.i;
ASSERT_NE(-1, (fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0644)));
EXPECT_EQ(5, write(fd, "hello", 5));
EXPECT_NE(-1, fdatasync(fd));
EXPECT_NE(MAP_FAILED, (p = mmap(NULL, 5, PROT_READ, MAP_PRIVATE, fd, 0)));
EXPECT_EQ(m1 + 1, _mm.i);
EXPECT_STREQ("hello", p);
EXPECT_NE(-1, munmap(p, 5));
EXPECT_EQ(m1 + 0, _mm.i);
EXPECT_NE(-1, close(fd));
EXPECT_NE(-1, unlink(path));
}
@ -106,14 +53,12 @@ TEST(mmap, testMapFile_fdGetsClosed_makesNoDifference) {
int fd;
char *p, buf[16], path[PATH_MAX];
sprintf(path, "%s%s.%d", kTmpPath, program_invocation_short_name, getpid());
m1 = _mm.i;
ASSERT_NE(-1, (fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0644)));
EXPECT_EQ(5, write(fd, "hello", 5));
EXPECT_NE(-1, fdatasync(fd));
EXPECT_NE(MAP_FAILED,
(p = mmap(NULL, 5, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)));
EXPECT_NE(-1, close(fd));
EXPECT_EQ(m1 + 1, _mm.i);
EXPECT_STREQ("hello", p);
p[1] = 'a';
EXPECT_NE(-1, msync(p, PAGESIZE, MS_SYNC));
@ -122,12 +67,11 @@ TEST(mmap, testMapFile_fdGetsClosed_makesNoDifference) {
EXPECT_STREQN("hallo", buf, 5);
EXPECT_NE(-1, close(fd));
EXPECT_NE(-1, munmap(p, 5));
EXPECT_EQ(m1 + 0, _mm.i);
EXPECT_NE(-1, unlink(path));
}
TEST(mmap, testMapFixed_destroysEverythingInItsPath) {
m1 = _mm.i;
unsigned m1 = _mmi.i;
EXPECT_NE(MAP_FAILED, mmap((void *)(kFixedMappingsStart + FRAMESIZE * 0),
FRAMESIZE, PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
@ -137,10 +81,31 @@ TEST(mmap, testMapFixed_destroysEverythingInItsPath) {
EXPECT_NE(MAP_FAILED, mmap((void *)(kFixedMappingsStart + FRAMESIZE * 2),
FRAMESIZE, PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
ASSERT_EQ(m1 + 3, _mm.i);
EXPECT_NE(MAP_FAILED, mmap((void *)(kFixedMappingsStart + FRAMESIZE * 0),
FRAMESIZE * 3, PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
ASSERT_EQ(m1 + 1, _mm.i);
ASSERT_GT(_mmi.i, m1);
EXPECT_NE(-1, munmap((void *)kFixedMappingsStart, FRAMESIZE * 3));
ASSERT_EQ(m1, _mmi.i);
}
TEST(isheap, nullPtr) {
ASSERT_FALSE(isheap(NULL));
}
TEST(isheap, stackMemory) {
int boop;
ASSERT_FALSE(isheap(&boop));
}
TEST(isheap, malloc) {
ASSERT_TRUE(isheap(gc(malloc(1))));
}
TEST(isheap, emptyMalloc) {
ASSERT_TRUE(isheap(gc(malloc(0))));
}
TEST(isheap, mallocOffset) {
ASSERT_TRUE(isheap((char *)gc(malloc(131072)) + 100000));
}

View file

@ -5,12 +5,14 @@ PKGS += TEST_LIBC_RUNTIME
TEST_LIBC_RUNTIME_SRCS := $(wildcard test/libc/runtime/*.c)
TEST_LIBC_RUNTIME_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_RUNTIME_SRCS))
TEST_LIBC_RUNTIME_COMS = $(TEST_LIBC_RUNTIME_OBJS:%.o=%.com)
TEST_LIBC_RUNTIME_OBJS = \
$(TEST_LIBC_RUNTIME_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_RUNTIME_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_RUNTIME_COMS = \
$(TEST_LIBC_RUNTIME_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_RUNTIME_BINS = \
$(TEST_LIBC_RUNTIME_COMS) \
$(TEST_LIBC_RUNTIME_COMS:%=%.dbg)
@ -22,13 +24,16 @@ TEST_LIBC_RUNTIME_CHECKS = \
$(TEST_LIBC_RUNTIME_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
TEST_LIBC_RUNTIME_DIRECTDEPS = \
LIBC_ALG \
LIBC_CALLS \
LIBC_CALLS_HEFTY \
LIBC_FMT \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_RAND \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_LOG \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
@ -60,6 +65,7 @@ o/$(MODE)/test/libc/runtime/getenv_test.com.runs: \
o/$(MODE)/test/libc/runtime/getenv_test.com
@HELLO=THERE build/runit $@ $<
o/$(MODE)/test/libc/runtime/fun_test.o \
o/$(MODE)/test/libc/runtime/itsatrap_test.o: \
OVERRIDE_CFLAGS += \
-fno-sanitize=all \

View file

@ -5,12 +5,14 @@ PKGS += TEST_LIBC_SOCK
TEST_LIBC_SOCK_SRCS := $(wildcard test/libc/sock/*.c)
TEST_LIBC_SOCK_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_SOCK_SRCS))
TEST_LIBC_SOCK_COMS = $(TEST_LIBC_SOCK_OBJS:%.o=%.com)
TEST_LIBC_SOCK_OBJS = \
$(TEST_LIBC_SOCK_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_SOCK_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_SOCK_COMS = \
$(TEST_LIBC_SOCK_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_SOCK_BINS = \
$(TEST_LIBC_SOCK_COMS) \
$(TEST_LIBC_SOCK_COMS:%=%.dbg)
@ -51,9 +53,7 @@ o/$(MODE)/test/libc/sock/%.com.dbg: \
$(APE)
@$(APELINK)
$(TEST_LIBC_SOCK_OBJS): \
$(BUILD_FILES) \
test/libc/sock/test.mk
$(TEST_LIBC_SOCK_OBJS): test/libc/sock/test.mk
.PHONY: o/$(MODE)/test/libc/sock
o/$(MODE)/test/libc/sock: \

View file

@ -5,12 +5,14 @@ PKGS += TEST_LIBC_STDIO
TEST_LIBC_STDIO_SRCS := $(wildcard test/libc/stdio/*.c)
TEST_LIBC_STDIO_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_STDIO_SRCS))
TEST_LIBC_STDIO_COMS = $(TEST_LIBC_STDIO_OBJS:%.o=%.com)
TEST_LIBC_STDIO_OBJS = \
$(TEST_LIBC_STDIO_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_STDIO_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_STDIO_COMS = \
$(TEST_LIBC_STDIO_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_STDIO_BINS = \
$(TEST_LIBC_STDIO_COMS) \
$(TEST_LIBC_STDIO_COMS:%=%.dbg)

View file

@ -18,6 +18,7 @@
02110-1301 USA
*/
#include "libc/dce.h"
#include "libc/nexgen32e/crc32.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
@ -40,9 +41,10 @@ TEST(crc32c, test) {
strlen(hyperion) - strlen(FANATICS)));
}
uint32_t crc32c$pure(uint32_t, const char *, size_t) hidden;
uint32_t crc32c$sse42(uint32_t, const char *, size_t) hidden;
FIXTURE(crc32c, pure) { *(void **)(&crc32c) = (void *)crc32c$pure; }
FIXTURE(crc32c, pure) {
*(void **)(&crc32c) = (void *)crc32c$pure;
}
FIXTURE(crc32c, sse42) {
if (X86_HAVE(SSE4_2)) {
*(void **)(&crc32c) = (void *)crc32c$sse42;

View file

@ -17,9 +17,14 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/str/tinymemccpy.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
void *memccpy2(void *, const void *, int, size_t);
TEST(memccpy, testStringCopy) {
char buf[16];
EXPECT_EQ(buf + 3, memccpy(buf, "hi", '\0', sizeof(buf)));
@ -35,3 +40,21 @@ TEST(memccpy, testZeroLength_doesNothing) {
char buf[1];
EXPECT_EQ(NULL, memccpy(buf, "hi", '\0', 0));
}
TEST(memccpy, memcpy) {
unsigned n, n2;
char *b1, *b2, *b3, *e1, *e2;
for (n = 0; n < 1026; ++n) {
b1 = tmalloc(n);
b2 = tmalloc(n);
b3 = tmalloc(n);
e1 = tinymemccpy(b2, b1, 31337, n);
e2 = memccpy(b3, b1, 31337, n);
n2 = e1 ? e1 - b1 : n;
ASSERT_EQ(e1, e2);
ASSERT_EQ(0, memcmp(b2, b3, n2));
tfree(b3);
tfree(b2);
tfree(b1);
}
}

View file

@ -17,30 +17,17 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/intrin/phaddsw.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
#include "tool/viz/lib/formatstringtable-testlib.h"
/* clang-format off */
#include "third_party/regex/regex.h"
FIXTURE(phaddsw, disableHardwareExtensions) {
memset((/*unconst*/ void *)kCpuids, 0, sizeof(kCpuids));
}
TEST(phaddsw, testOverflow_saturates) {
short M[2][8] = {
{0x7fff, 0, 0x7fff, 1, 0x7fff, 0x7fff, 20777, -16389},
{-28040, 13318, -1336, -24798, -13876, 3599, -7346, -23575},
};
phaddsw(M[0], M[0], M[1]);
EXPECT_SHRTMATRIXEQ(2, 8, M, "\n\
32767 32767 32767 4388 -14722 -26134 -10277 -30921\n\
-28040 13318 -1336 -24798 -13876 3599 -7346 -23575");
}
TEST(phaddsw, testAliasing_isOk) {
short M[1][8] = {{0,1,2,3,4,5,6,7}};
phaddsw(M[0],M[0],M[0]);
EXPECT_SHRTMATRIXEQ(1, 8, M, "\n\
1 5 9 13 1 5 9 13");
TEST(regex, test) {
regex_t rx;
EXPECT_EQ(REG_OK, regcomp(&rx, "^[A-Za-z\x7f-\uffff]{2}$", REG_EXTENDED));
EXPECT_EQ(REG_OK, regexec(&rx, "AZ", 0, NULL, 0));
EXPECT_EQ(REG_OK, regexec(&rx, "→→", 0, NULL, 0));
EXPECT_EQ(REG_NOMATCH, regexec(&rx, "A", 0, NULL, 0));
EXPECT_EQ(REG_NOMATCH, regexec(&rx, "", 0, NULL, 0));
EXPECT_EQ(REG_NOMATCH, regexec(&rx, "0", 0, NULL, 0));
regfree(&rx);
}

View file

@ -18,6 +18,7 @@
02110-1301 USA
*/
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/hyperion.h"
#include "libc/testlib/testlib.h"
@ -30,8 +31,16 @@ uint8_t *sha256(const char *s) {
return hash;
}
TEST(sha256, testEmpty) {
EXPECT_BINEQ(
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
sha256(""));
}
TEST(sha256, test) {
EXPECT_BINEQ(u",≥M║_░ú♫&Φ;*┼╣Γ€←▬▲╲▼ºB^s♦3bôïÿ$", sha256("hello"));
EXPECT_BINEQ(u",≥M║_░ú♫&Φ;*┼╣Γ€←▬▲\\▼ºB^s♦3bôïÿ$", sha256("hello"));
EXPECT_BINEQ("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b98",
sha256("hello"));
}
TEST(sha256, testNontrivialSize) {

View file

@ -551,7 +551,8 @@ void longstringislong_dupe(size_t size, char data[size], char dupe[size]) {
BENCH(bench_00_strcmp, bench) {
size_t size;
char *dupe, *data;
size = ROUNDDOWN(getcachesize(kCpuCacheTypeData, 1) / 2, PAGESIZE);
size = ROUNDDOWN(MAX(FRAMESIZE, getcachesize(kCpuCacheTypeData, 1)) / 2,
PAGESIZE);
data = tgc(tmalloc(size));
dupe = tgc(tmalloc(size));
EZBENCH2("strcmp [identity]", longstringislong(size, data),
@ -569,7 +570,8 @@ BENCH(bench_00_strcmp, bench) {
BENCH(bench_01_strcasecmp, bench) {
size_t size;
char *dupe, *data;
size = ROUNDDOWN(getcachesize(kCpuCacheTypeData, 1) / 2, PAGESIZE);
size = ROUNDDOWN(MAX(FRAMESIZE, getcachesize(kCpuCacheTypeData, 1)) / 2,
PAGESIZE);
data = tgc(tmalloc(size));
dupe = tgc(tmalloc(size));
EZBENCH2("strcasecmp [identity]", longstringislong(size, data),

View file

@ -86,7 +86,7 @@ TEST(strnlen, nulNotFound_ReturnsSize) {
for (unsigned i = 0; i < ARRAYLEN(sizes); ++i) {
char *buf = tmalloc(sizes[i]);
memset(buf, ' ', sizes[i]);
ASSERT_EQ(sizes[i], strnlen(buf, sizes[i]));
ASSERT_EQ(sizes[i], strnlen(buf, sizes[i]), "%d", sizes[i]);
tfree(buf);
}
}

View file

@ -21,25 +21,29 @@
#include "libc/testlib/testlib.h"
#define T(NAME) NAME
#define S(S) S
#define C(C) C
#define S(S) S
#define C(C) C
#include "test/libc/str/strrchr_test.inc"
#undef C
#undef S
#undef T
#define T(NAME) NAME##16
#define S(S) u##S
#define C(C) u##C
#define T(NAME) NAME##16
#define S(S) u##S
#define C(C) u##C
#define strchr(x, y) strchr16(x, y)
#include "test/libc/str/strrchr_test.inc"
#undef strchr
#undef C
#undef S
#undef T
#define T(NAME) NAME##32
#define S(S) L##S
#define C(C) L##C
#define T(NAME) NAME##32
#define S(S) L##S
#define C(C) L##C
#define strchr(x, y) wcschr(x, y)
#include "test/libc/str/strrchr_test.inc"
#undef strchr
#undef C
#undef S
#undef T

View file

@ -5,12 +5,14 @@ PKGS += TEST_LIBC_STR
TEST_LIBC_STR_SRCS := $(wildcard test/libc/str/*.c)
TEST_LIBC_STR_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_STR_SRCS))
TEST_LIBC_STR_COMS = $(TEST_LIBC_STR_OBJS:%.o=%.com)
TEST_LIBC_STR_OBJS = \
$(TEST_LIBC_STR_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_STR_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_STR_COMS = \
$(TEST_LIBC_STR_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_STR_BINS = \
$(TEST_LIBC_STR_COMS) \
$(TEST_LIBC_STR_COMS:%=%.dbg)
@ -38,6 +40,7 @@ TEST_LIBC_STR_DIRECTDEPS = \
LIBC_LOG \
LIBC_X \
LIBC_ZIPOS \
THIRD_PARTY_REGEX \
THIRD_PARTY_ZLIB
TEST_LIBC_STR_DEPS := \

View file

@ -23,6 +23,7 @@
#include "libc/log/check.h"
#include "libc/macros.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/crc32.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/rbx.h"
#include "libc/runtime/runtime.h"
@ -50,6 +51,7 @@ TEST(undeflate, testEmbeddedPlaintextConstant) {
TEST(undeflate, testStatCentralDirectory_notFound_noSysCalls) {
uint64_t c;
struct stat st;
stat("zip:doge.txt", &st); /* warmup */
c = g_syscount;
ASSERT_EQ(-1, stat("zip:doge.txt", &st));
ASSERT_EQ(0, g_syscount - c);

View file

@ -1,301 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/fmt/bing.h"
#include "libc/limits.h"
#include "libc/macros.h"
#include "libc/runtime/gc.h"
#include "libc/str/str.h"
#include "libc/str/varint.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
#define TV(X, B, I) __FILE__, __LINE__, #X, X, #B, B, #I, I
static const struct VarintTestVectors {
const char *file;
int line;
const char *xcode;
uint64_t x;
const char *bcode;
uint16_t b[11];
const char *icode;
uint8_t i;
} kv[] = {
{TV(0x0000000000000000ul, u"          ", 1)},
{TV(0x0000000000000001ul, u"☺         ", 1)},
{TV(0x0000000000000003ul, u"♥         ", 1)},
{TV(0x0000000000000007ul, u"•         ", 1)},
{TV(0x000000000000000ful, u"☼         ", 1)},
{TV(0x000000000000001ful, u"▼         ", 1)},
{TV(0x000000000000003ful, u"⁇         ", 1)},
{TV(0x000000000000007ful, u"⌂         ", 1)},
{TV(0x00000000000000fful, u"λ☺        ", 2)},
{TV(0x00000000000001fful, u"λ♥        ", 2)},
{TV(0x00000000000003fful, u"λ•        ", 2)},
{TV(0x00000000000007fful, u"λ☼        ", 2)},
{TV(0x0000000000000ffful, u"λ▼        ", 2)},
{TV(0x0000000000001ffful, u"λ⁇        ", 2)},
{TV(0x0000000000003ffful, u"λ⌂        ", 2)},
{TV(0x0000000000007ffful, u"λλ☺       ", 3)},
{TV(0x000000000000fffful, u"λλ♥       ", 3)},
{TV(0x000000000001fffful, u"λλ•       ", 3)},
{TV(0x000000000003fffful, u"λλ☼       ", 3)},
{TV(0x000000000007fffful, u"λλ▼       ", 3)},
{TV(0x00000000000ffffful, u"λλ⁇       ", 3)},
{TV(0x00000000001ffffful, u"λλ⌂       ", 3)},
{TV(0x00000000003ffffful, u"λλλ☺      ", 4)},
{TV(0x00000000007ffffful, u"λλλ♥      ", 4)},
{TV(0x0000000000fffffful, u"λλλ•      ", 4)},
{TV(0x0000000001fffffful, u"λλλ☼      ", 4)},
{TV(0x0000000003fffffful, u"λλλ▼      ", 4)},
{TV(0x0000000007fffffful, u"λλλ⁇      ", 4)},
{TV(0x000000000ffffffful, u"λλλ⌂      ", 4)},
{TV(0x000000001ffffffful, u"λλλλ☺     ", 5)},
{TV(0x000000003ffffffful, u"λλλλ♥     ", 5)},
{TV(0x000000007ffffffful, u"λλλλ•     ", 5)},
{TV(0x00000000fffffffful, u"λλλλ☼     ", 5)},
{TV(0x00000001fffffffful, u"λλλλ▼     ", 5)},
{TV(0x00000003fffffffful, u"λλλλ⁇     ", 5)},
{TV(0x00000007fffffffful, u"λλλλ⌂     ", 5)},
{TV(0x0000000ffffffffful, u"λλλλλ☺    ", 6)},
{TV(0x0000001ffffffffful, u"λλλλλ♥    ", 6)},
{TV(0x0000003ffffffffful, u"λλλλλ•    ", 6)},
{TV(0x0000007ffffffffful, u"λλλλλ☼    ", 6)},
{TV(0x000000fffffffffful, u"λλλλλ▼    ", 6)},
{TV(0x000001fffffffffful, u"λλλλλ⁇    ", 6)},
{TV(0x000003fffffffffful, u"λλλλλ⌂    ", 6)},
{TV(0x000007fffffffffful, u"λλλλλλ☺   ", 7)},
{TV(0x00000ffffffffffful, u"λλλλλλ♥   ", 7)},
{TV(0x00001ffffffffffful, u"λλλλλλ•   ", 7)},
{TV(0x00003ffffffffffful, u"λλλλλλ☼   ", 7)},
{TV(0x00007ffffffffffful, u"λλλλλλ▼   ", 7)},
{TV(0x0000fffffffffffful, u"λλλλλλ⁇   ", 7)},
{TV(0x0001fffffffffffful, u"λλλλλλ⌂   ", 7)},
{TV(0x0003fffffffffffful, u"λλλλλλλ☺  ", 8)},
{TV(0x0007fffffffffffful, u"λλλλλλλ♥  ", 8)},
{TV(0x000ffffffffffffful, u"λλλλλλλ•  ", 8)},
{TV(0x001ffffffffffffful, u"λλλλλλλ☼  ", 8)},
{TV(0x003ffffffffffffful, u"λλλλλλλ▼  ", 8)},
{TV(0x007ffffffffffffful, u"λλλλλλλ⁇  ", 8)},
{TV(0x00fffffffffffffful, u"λλλλλλλ⌂  ", 8)},
{TV(0x01fffffffffffffful, u"λλλλλλλλ☺ ", 9)},
{TV(0x03fffffffffffffful, u"λλλλλλλλ♥ ", 9)},
{TV(0x07fffffffffffffful, u"λλλλλλλλ• ", 9)},
{TV(0x0ffffffffffffffful, u"λλλλλλλλ☼ ", 9)},
{TV(0x1ffffffffffffffful, u"λλλλλλλλ▼ ", 9)},
{TV(0x3ffffffffffffffful, u"λλλλλλλλ⁇ ", 9)},
{TV(0x7ffffffffffffffful, u"λλλλλλλλ⌂ ", 9)},
{TV(0xfffffffffffffffful, u"λλλλλλλλλ☺", 10)},
};
const struct ZigzagTestVectors {
const char *file;
int line;
const char *xcode;
int64_t x;
const char *bcode;
uint16_t b[11];
const char *icode;
uint8_t i;
} kz[] = {
{TV(0, u" ", 1)},
{TV(1, u"", 1)},
{TV(-1, u"", 1)},
{TV(2, u"", 1)},
{TV(-2, u"", 1)},
{TV(4, u"", 1)},
{TV(-4, u"", 1)},
{TV(8, u"", 1)},
{TV(-8, u"", 1)},
{TV(16, u" ", 1)},
{TV(-16, u"", 1)},
{TV(32, u"@", 1)},
{TV(-32, u"", 1)},
{TV(64, u"Ç☺", 2)},
{TV(-64, u"", 1)},
{TV(128, u"Ç☻", 2)},
{TV(-128, u"λ☺", 2)},
{TV(256, u"Ç♦", 2)},
{TV(-256, u"λ♥", 2)},
{TV(512, u"Ç◘", 2)},
{TV(-512, u"λ•", 2)},
{TV(1024, u"Ç►", 2)},
{TV(-1024, u"λ☼", 2)},
{TV(2048, u"Ç ", 2)},
{TV(-2048, u"λ▼", 2)},
{TV(4096, u"Ç@", 2)},
{TV(-4096, u"λ⁇", 2)},
{TV(8192, u"ÇÇ☺", 3)},
{TV(-8192, u"λ⌂", 2)},
{TV(16384, u"ÇÇ☻", 3)},
{TV(-16384, u"λλ☺", 3)},
{TV(32768, u"ÇÇ♦", 3)},
{TV(-32768, u"λλ♥", 3)},
{TV(65536, u"ÇÇ◘", 3)},
{TV(-65536, u"λλ•", 3)},
{TV(131072, u"ÇÇ►", 3)},
{TV(-131072, u"λλ☼", 3)},
{TV(262144, u"ÇÇ ", 3)},
{TV(-262144, u"λλ▼", 3)},
{TV(524288, u"ÇÇ@", 3)},
{TV(-524288, u"λλ⁇", 3)},
{TV(1048576, u"ÇÇÇ☺", 4)},
{TV(-1048576, u"λλ⌂", 3)},
{TV(2097152, u"ÇÇÇ☻", 4)},
{TV(-2097152, u"λλλ☺", 4)},
{TV(4194304, u"ÇÇÇ♦", 4)},
{TV(-4194304, u"λλλ♥", 4)},
{TV(8388608, u"ÇÇÇ◘", 4)},
{TV(-8388608, u"λλλ•", 4)},
{TV(16777216, u"ÇÇÇ►", 4)},
{TV(-16777216, u"λλλ☼", 4)},
{TV(33554432, u"ÇÇÇ ", 4)},
{TV(-33554432, u"λλλ▼", 4)},
{TV(67108864, u"ÇÇÇ@", 4)},
{TV(-67108864, u"λλλ⁇", 4)},
{TV(134217728, u"ÇÇÇÇ☺", 5)},
{TV(-134217728, u"λλλ⌂", 4)},
{TV(268435456, u"ÇÇÇÇ☻", 5)},
{TV(-268435456, u"λλλλ☺", 5)},
{TV(536870912, u"ÇÇÇÇ♦", 5)},
{TV(-536870912, u"λλλλ♥", 5)},
{TV(1073741824, u"ÇÇÇÇ◘", 5)},
{TV(-1073741824, u"λλλλ•", 5)},
{TV(2147483648, u"ÇÇÇÇ►", 5)},
{TV(-2147483648, u"λλλλ☼", 5)},
{TV(4294967296, u"ÇÇÇÇ ", 5)},
{TV(-4294967296, u"λλλλ▼", 5)},
{TV(8589934592, u"ÇÇÇÇ@", 5)},
{TV(-8589934592, u"λλλλ⁇", 5)},
{TV(17179869184, u"ÇÇÇÇÇ☺", 6)},
{TV(-17179869184, u"λλλλ⌂", 5)},
{TV(34359738368, u"ÇÇÇÇÇ☻", 6)},
{TV(-34359738368, u"λλλλλ☺", 6)},
{TV(68719476736, u"ÇÇÇÇÇ♦", 6)},
{TV(-68719476736, u"λλλλλ♥", 6)},
{TV(137438953472, u"ÇÇÇÇÇ◘", 6)},
{TV(-137438953472, u"λλλλλ•", 6)},
{TV(274877906944, u"ÇÇÇÇÇ►", 6)},
{TV(-274877906944, u"λλλλλ☼", 6)},
{TV(549755813888, u"ÇÇÇÇÇ ", 6)},
{TV(-549755813888, u"λλλλλ▼", 6)},
{TV(1099511627776, u"ÇÇÇÇÇ@", 6)},
{TV(-1099511627776, u"λλλλλ⁇", 6)},
{TV(2199023255552, u"ÇÇÇÇÇÇ☺", 7)},
{TV(-2199023255552, u"λλλλλ⌂", 6)},
{TV(4398046511104, u"ÇÇÇÇÇÇ☻", 7)},
{TV(-4398046511104, u"λλλλλλ☺", 7)},
{TV(8796093022208, u"ÇÇÇÇÇÇ♦", 7)},
{TV(-8796093022208, u"λλλλλλ♥", 7)},
{TV(17592186044416, u"ÇÇÇÇÇÇ◘", 7)},
{TV(-17592186044416, u"λλλλλλ•", 7)},
{TV(35184372088832, u"ÇÇÇÇÇÇ►", 7)},
{TV(-35184372088832, u"λλλλλλ☼", 7)},
{TV(70368744177664, u"ÇÇÇÇÇÇ ", 7)},
{TV(-70368744177664, u"λλλλλλ▼", 7)},
{TV(140737488355328, u"ÇÇÇÇÇÇ@", 7)},
{TV(-140737488355328, u"λλλλλλ⁇", 7)},
{TV(281474976710656, u"ÇÇÇÇÇÇÇ☺", 8)},
{TV(-281474976710656, u"λλλλλλ⌂", 7)},
{TV(562949953421312, u"ÇÇÇÇÇÇÇ☻", 8)},
{TV(-562949953421312, u"λλλλλλλ☺", 8)},
{TV(1125899906842624, u"ÇÇÇÇÇÇÇ♦", 8)},
{TV(-1125899906842624, u"λλλλλλλ♥", 8)},
{TV(2251799813685248, u"ÇÇÇÇÇÇÇ◘", 8)},
{TV(-2251799813685248, u"λλλλλλλ•", 8)},
{TV(4503599627370496, u"ÇÇÇÇÇÇÇ►", 8)},
{TV(-4503599627370496, u"λλλλλλλ☼", 8)},
{TV(9007199254740992, u"ÇÇÇÇÇÇÇ ", 8)},
{TV(-9007199254740992, u"λλλλλλλ▼", 8)},
{TV(18014398509481984, u"ÇÇÇÇÇÇÇ@", 8)},
{TV(-18014398509481984, u"λλλλλλλ⁇", 8)},
{TV(36028797018963968, u"ÇÇÇÇÇÇÇÇ☺", 9)},
{TV(-36028797018963968, u"λλλλλλλ⌂", 8)},
{TV(72057594037927936, u"ÇÇÇÇÇÇÇÇ☻", 9)},
{TV(-72057594037927936, u"λλλλλλλλ☺", 9)},
{TV(144115188075855872, u"ÇÇÇÇÇÇÇÇ♦", 9)},
{TV(-144115188075855872, u"λλλλλλλλ♥", 9)},
{TV(288230376151711744, u"ÇÇÇÇÇÇÇÇ◘", 9)},
{TV(-288230376151711744, u"λλλλλλλλ•", 9)},
{TV(576460752303423488, u"ÇÇÇÇÇÇÇÇ►", 9)},
{TV(-576460752303423488, u"λλλλλλλλ☼", 9)},
{TV(1152921504606846976, u"ÇÇÇÇÇÇÇÇ ", 9)},
{TV(-1152921504606846976, u"λλλλλλλλ▼", 9)},
{TV(2305843009213693952, u"ÇÇÇÇÇÇÇÇ@", 9)},
{TV(-2305843009213693952, u"λλλλλλλλ⁇", 9)},
{TV(4611686018427387904, u"ÇÇÇÇÇÇÇÇÇ☺", 10)},
{TV(-4611686018427387904, u"λλλλλλλλ⌂", 9)},
{TV(INT64_MIN, u"λλλλλλλλλ☺", 10)},
{TV(INT64_MAX, u"■λλλλλλλλ☺", 10)}, /* wut */
};
TEST(writevint, test) {
size_t i;
uint8_t b[10], *p;
for (i = 0; i < ARRAYLEN(kv); ++i) {
memset(b, 0, sizeof(b));
p = writevint(b, kv[i].x);
__TEST_EQ(assert, kz[i].file, kz[i].line, __FUNCTION__, kz[i].icode,
kz[i].xcode, kv[i].i, p - b);
assertBinaryEquals$cp437(kz[i].file, kz[i].line, __FUNCTION__, kv[i].b, b,
strlen(kv[i].b), "", false);
}
}
TEST(readvint, test) {
size_t i;
uint64_t x;
uint8_t *b, *p;
for (i = 0; i < ARRAYLEN(kv); ++i) {
b = gc(unbingstr(kv[i].b));
p = readvint(b, b + strlen(kv[i].b), &x);
__TEST_EQ(assert, kv[i].file, kv[i].line, __FUNCTION__, kv[i].icode,
kv[i].xcode, kv[i].i, (intptr_t)(p - b));
assertBinaryEquals$cp437(kv[i].file, kv[i].line, __FUNCTION__, kv[i].b, b,
strlen(kv[i].b), "", false);
}
}
TEST(writesint, test) {
size_t i;
uint8_t b[10], *p;
for (i = 0; i < ARRAYLEN(kz); ++i) {
memset(b, 0, sizeof(b));
p = writesint(b, kz[i].x);
__TEST_EQ(assert, kz[i].file, kz[i].line, __FUNCTION__, kz[i].icode,
kz[i].xcode, kz[i].i, (intptr_t)(p - b));
assertBinaryEquals$cp437(kz[i].file, kz[i].line, __FUNCTION__, kz[i].b, b,
strlen(kz[i].b), "", false);
}
}
TEST(readsint, test) {
size_t i;
int64_t x;
uint8_t *b, *p;
for (i = 0; i < ARRAYLEN(kz); ++i) {
b = gc(unbingstr(kz[i].b));
p = readsint(b, b + strlen(kz[i].b), &x);
__TEST_EQ(assert, kz[i].file, kz[i].line, __FUNCTION__, kz[i].icode,
kz[i].xcode, kz[i].i, (intptr_t)(p - b));
assertBinaryEquals$cp437(kz[i].file, kz[i].line, __FUNCTION__, kz[i].b, b,
strlen(kz[i].b), "", false);
}
}

View file

@ -30,7 +30,7 @@
TEST(fastdiv, test) {
long x = 123000000321;
EXPECT_EQ(123, div1000000000int64(x));
EXPECT_EQ(321, mod1000000000int64(x));
EXPECT_EQ(321, rem1000000000int64(x));
}
TEST(dsleep, test) {

View file

@ -5,13 +5,15 @@ PKGS += TEST_LIBC_TIME
TEST_LIBC_TIME_SRCS := $(wildcard test/libc/time/*.c)
TEST_LIBC_TIME_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_TIME_SRCS))
TEST_LIBC_TIME_COMS = $(TEST_LIBC_TIME_OBJS:%.o=%.com)
TEST_LIBC_TIME_BINS = $(TEST_LIBC_TIME_COMS) $(TEST_LIBC_TIME_COMS:%=%.dbg)
TEST_LIBC_TIME_OBJS = \
$(TEST_LIBC_TIME_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_TIME_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_TIME_COMS = \
$(TEST_LIBC_TIME_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_TIME_TESTS = $(TEST_LIBC_TIME_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
TEST_LIBC_TIME_CHECKS = \
$(TEST_LIBC_TIME_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)

View file

@ -17,34 +17,37 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/conv/sizemultiply.h"
#include "libc/limits.h"
#include "libc/math.h"
#include "libc/testlib/testlib.h"
#include "libc/tinymath/tinymath.h"
size_t out;
TEST(sizemultiply, testMultiplication) {
EXPECT_TRUE(sizemultiply(&out, 7, 11));
EXPECT_EQ(7 * 11, out);
EXPECT_TRUE(sizemultiply(&out, 11, 7));
EXPECT_EQ(7 * 11, out);
EXPECT_TRUE(sizemultiply(&out, 0, 11));
EXPECT_EQ(0 * 11, out);
EXPECT_TRUE(sizemultiply(&out, 11, 0));
EXPECT_EQ(0 * 11, out);
TEST(ilogb, yolo) {
EXPECT_EQ(0, ilogb(1));
EXPECT_EQ(1, ilogb(2));
EXPECT_EQ(2, ilogb(4));
EXPECT_EQ(63, ilogb(1e19));
EXPECT_EQ(0, ilogbf(1));
EXPECT_EQ(1, ilogbf(2));
EXPECT_EQ(2, ilogbf(4));
EXPECT_EQ(63, ilogb(1e19));
EXPECT_EQ(0, ilogbl(1));
EXPECT_EQ(1, ilogbl(2));
EXPECT_EQ(2, ilogbl(4));
EXPECT_EQ(63, ilogbl(1e19));
}
TEST(sizemultiply, testOverflow_alwaysReturnsSizeMax) {
EXPECT_FALSE(sizemultiply(&out, 7, SIZE_MAX));
EXPECT_EQ(SIZE_MAX, out);
EXPECT_FALSE(sizemultiply(&out, SIZE_MAX, 7));
EXPECT_EQ(SIZE_MAX, out);
}
TEST(sizemultiply, testOverflow_closeButNoCigar) {
EXPECT_TRUE(sizemultiply(&out, SIZE_MAX, 1));
EXPECT_EQ(SIZE_MAX, out);
EXPECT_TRUE(sizemultiply(&out, 1, SIZE_MAX));
EXPECT_EQ(SIZE_MAX, out);
TEST(logb, yolo) {
EXPECT_EQ(0, (int)logb(1));
EXPECT_EQ(1, (int)logb(2));
EXPECT_EQ(2, (int)logb(4));
EXPECT_EQ(63, (int)logb(1e19));
EXPECT_EQ(0, (int)logbf(1));
EXPECT_EQ(1, (int)logbf(2));
EXPECT_EQ(2, (int)logbf(4));
EXPECT_EQ(63, (int)logb(1e19));
EXPECT_EQ(0, (int)logbl(1));
EXPECT_EQ(1, (int)logbl(2));
EXPECT_EQ(2, (int)logbl(4));
EXPECT_EQ(63, (int)logbl(1e19));
}

View file

@ -27,15 +27,6 @@
float tinymath_roundf$k8(float);
double tinymath_round$k8(double);
TEST(round, test) {
EXPECT_STREQ("-3", gc(xdtoa(tinymath_round(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_round(-1.5))));
EXPECT_STREQ("-1", gc(xdtoa(tinymath_round(-.5))));
EXPECT_STREQ("1", gc(xdtoa(tinymath_round(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_round(1.5))));
EXPECT_STREQ("3", gc(xdtoa(tinymath_round(2.5))));
}
TEST(round, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(tinymath_round(-0.0))));
EXPECT_STREQ("nan", gc(xdtoa(tinymath_round(NAN))));
@ -44,6 +35,105 @@ TEST(round, testCornerCases) {
EXPECT_STREQ("-inf", gc(xdtoa(tinymath_round(-INFINITY))));
}
TEST(round, test) {
EXPECT_STREQ("-3", gc(xdtoa(tinymath_round(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_round(-1.5))));
EXPECT_STREQ("-1", gc(xdtoa(tinymath_round(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_round(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_round(.4))));
EXPECT_STREQ("1", gc(xdtoa(tinymath_round(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_round(1.5))));
EXPECT_STREQ("3", gc(xdtoa(tinymath_round(2.5))));
}
TEST(roundf, test) {
EXPECT_STREQ("-3", gc(xdtoa(tinymath_roundf(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_roundf(-1.5))));
EXPECT_STREQ("-1", gc(xdtoa(tinymath_roundf(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_roundf(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_roundf(.4))));
EXPECT_STREQ("1", gc(xdtoa(tinymath_roundf(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_roundf(1.5))));
EXPECT_STREQ("3", gc(xdtoa(tinymath_roundf(2.5))));
}
TEST(roundl, test) {
EXPECT_STREQ("-3", gc(xdtoa(tinymath_roundl(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_roundl(-1.5))));
EXPECT_STREQ("-1", gc(xdtoa(tinymath_roundl(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_roundl(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_roundl(.4))));
EXPECT_STREQ("1", gc(xdtoa(tinymath_roundl(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_roundl(1.5))));
EXPECT_STREQ("3", gc(xdtoa(tinymath_roundl(2.5))));
}
TEST(nearbyint, test) {
EXPECT_STREQ("-2", gc(xdtoa(tinymath_nearbyint(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_nearbyint(-1.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_nearbyint(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_nearbyint(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_nearbyint(.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_nearbyint(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_nearbyint(1.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_nearbyint(2.5))));
}
TEST(nearbyintf, test) {
EXPECT_STREQ("-2", gc(xdtoa(tinymath_nearbyintf(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_nearbyintf(-1.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_nearbyintf(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_nearbyintf(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_nearbyintf(.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_nearbyintf(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_nearbyintf(1.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_nearbyintf(2.5))));
}
TEST(nearbyintl, test) {
EXPECT_STREQ("-2", gc(xdtoa(tinymath_nearbyintl(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_nearbyintl(-1.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_nearbyintl(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_nearbyintl(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_nearbyintl(.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_nearbyintl(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_nearbyintl(1.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_nearbyintl(2.5))));
}
TEST(rint, test) {
EXPECT_STREQ("-2", gc(xdtoa(tinymath_rint(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_rint(-1.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_rint(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_rint(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_rint(.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_rint(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_rint(1.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_rint(2.5))));
}
TEST(rintf, test) {
EXPECT_STREQ("-2", gc(xdtoa(tinymath_rintf(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_rintf(-1.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_rintf(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_rintf(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_rintf(.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_rintf(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_rintf(1.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_rintf(2.5))));
}
TEST(rintl, test) {
EXPECT_STREQ("-2", gc(xdtoa(tinymath_rintl(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_rintl(-1.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_rintl(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_rintl(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_rintl(.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_rintl(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_rintl(1.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_rintl(2.5))));
}
TEST(lround, test) {
EXPECT_EQ(-3, tinymath_lround(-2.5));
EXPECT_EQ(-2, tinymath_lround(-1.5));
@ -54,15 +144,6 @@ TEST(lround, test) {
EXPECT_EQ(3, tinymath_lround(2.5));
}
TEST(roundf, test) {
EXPECT_STREQ("-3", gc(xdtoa(tinymath_roundf(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_roundf(-1.5))));
EXPECT_STREQ("-1", gc(xdtoa(tinymath_roundf(-.5))));
EXPECT_STREQ("1", gc(xdtoa(tinymath_roundf(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_roundf(1.5))));
EXPECT_STREQ("3", gc(xdtoa(tinymath_roundf(2.5))));
}
TEST(roundf, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(tinymath_roundf(-0.0))));
EXPECT_STREQ("nan", gc(xdtoa(tinymath_roundf(NAN))));

View file

@ -5,17 +5,20 @@ PKGS += TEST_LIBC_TINYMATH
TEST_LIBC_TINYMATH_SRCS := $(wildcard test/libc/tinymath/*.c)
TEST_LIBC_TINYMATH_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_TINYMATH_SRCS))
TEST_LIBC_TINYMATH_COMS = $(TEST_LIBC_TINYMATH_OBJS:%.o=%.com)
TEST_LIBC_TINYMATH_OBJS = \
$(TEST_LIBC_TINYMATH_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_TINYMATH_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_TINYMATH_COMS = \
$(TEST_LIBC_TINYMATH_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_TINYMATH_BINS = \
$(TEST_LIBC_TINYMATH_COMS) \
$(TEST_LIBC_TINYMATH_COMS:%=%.dbg)
TEST_LIBC_TINYMATH_TESTS = $(TEST_LIBC_TINYMATH_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
TEST_LIBC_TINYMATH_TESTS = \
$(TEST_LIBC_TINYMATH_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
TEST_LIBC_TINYMATH_CHECKS = \
$(TEST_LIBC_TINYMATH_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)

View file

@ -5,12 +5,14 @@ PKGS += TEST_LIBC_UNICODE
TEST_LIBC_UNICODE_SRCS := $(wildcard test/libc/unicode/*.c)
TEST_LIBC_UNICODE_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_UNICODE_SRCS))
TEST_LIBC_UNICODE_COMS = $(TEST_LIBC_UNICODE_OBJS:%.o=%.com)
TEST_LIBC_UNICODE_OBJS = \
$(TEST_LIBC_UNICODE_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_UNICODE_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_UNICODE_COMS = \
$(TEST_LIBC_UNICODE_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_UNICODE_BINS = \
$(TEST_LIBC_UNICODE_COMS) \
$(TEST_LIBC_UNICODE_COMS:%=%.dbg)

View file

@ -61,21 +61,6 @@ TEST(wcwidth, testCjkWidesAndCombiningLowLines_widthIsNotLength) {
/*────────────────────────────────────────────────────┴─*/
}
TEST(strwidth, testEmoji_cosmoHelpsYouBuildInclusiveProductsEasily) {
/* ┌─If this line is solid your terminal
is respectful and inclusive towards
our friends w/ rich and interesting
backgrounds that aren't Anglo-Saxon
This line being solid, means your
terminal needs a patch to address
issues concerning racism
*/
EXPECT_EQ(02, wcswidth(/**/ L"👦🏿" /*- │ - */));
/*────────────────────────────────────┼─┼─*/
}
TEST(strwidth, tab) {
EXPECT_EQ(32, strwidth("mov 0x0(%rip),%rcx \t"));
}

View file

@ -5,12 +5,14 @@ PKGS += TEST_LIBC_X
TEST_LIBC_X_SRCS := $(wildcard test/libc/x/*.c)
TEST_LIBC_X_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_X_SRCS))
TEST_LIBC_X_COMS = $(TEST_LIBC_X_OBJS:%.o=%.com)
TEST_LIBC_X_OBJS = \
$(TEST_LIBC_X_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_X_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_X_COMS = \
$(TEST_LIBC_X_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_X_BINS = \
$(TEST_LIBC_X_COMS) \
$(TEST_LIBC_X_COMS:%=%.dbg)
@ -29,7 +31,8 @@ TEST_LIBC_X_DIRECTDEPS = \
LIBC_RUNTIME \
LIBC_X \
LIBC_STUBS \
LIBC_TESTLIB
LIBC_TESTLIB \
THIRD_PARTY_DTOA
TEST_LIBC_X_DEPS := \
$(call uniq,$(foreach x,$(TEST_LIBC_X_DIRECTDEPS),$($(x))))

View file

@ -51,12 +51,14 @@ $(TEST_LIBC_XED_TESTLIB_A): \
PKGS += TEST_LIBC_XED
TEST_LIBC_XED_SRCS = $(filter %_test.c,$(TEST_LIBC_XED_FILES))
TEST_LIBC_XED_COMS = $(TEST_LIBC_XED_OBJS:%.o=%.com)
TEST_LIBC_XED_OBJS = \
$(TEST_LIBC_XED_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_XED_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_XED_COMS = \
$(TEST_LIBC_XED_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_XED_BINS = \
$(TEST_LIBC_XED_COMS) \
$(TEST_LIBC_XED_COMS:%=%.dbg)

View file

@ -41,7 +41,7 @@ testonly int ild(const char16_t *codez) {
error = xed_instruction_length_decode(
xed_decoded_inst_zero_set_mode(&xedd, XED_MACHINE_MODE_LONG_64),
gc(unbingx86op(codez)), strlen16(codez) + 16);
return error == XED_ERROR_NONE ? xedd.decoded_length : -error;
return error == XED_ERROR_NONE ? xedd.length : -error;
}
/**
@ -53,7 +53,7 @@ testonly int ildreal(const char16_t *codez) {
error = xed_instruction_length_decode(
xed_decoded_inst_zero_set_mode(&xedd, XED_MACHINE_MODE_REAL),
gc(unbingx86op(codez)), strlen16(codez) + 16);
return error == XED_ERROR_NONE ? xedd.decoded_length : -error;
return error == XED_ERROR_NONE ? xedd.length : -error;
}
/**
@ -65,5 +65,5 @@ testonly int ildlegacy(const char16_t *codez) {
error = xed_instruction_length_decode(
xed_decoded_inst_zero_set_mode(&xedd, XED_MACHINE_MODE_LEGACY_32),
gc(unbingx86op(codez)), strlen16(codez) + 16);
return error == XED_ERROR_NONE ? xedd.decoded_length : -error;
return error == XED_ERROR_NONE ? xedd.length : -error;
}

View file

@ -1416,7 +1416,7 @@ TEST(x86ild, test_415C) {
ISA_SET: I86
SHORT: pop r12
*/
EXPECT_EQ(2, ild(u"A"));
EXPECT_EQ(2, ild(u"A\\"));
}
TEST(x86ild, test_488B04C500000000) {

View file

@ -18,6 +18,7 @@
02110-1301 USA
*/
#include "libc/runtime/gc.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "test/libc/xed/lib.h"
#include "third_party/xed/x86.h"
@ -94,6 +95,24 @@ TEST(x86ild, testAmd3dnow) {
0, xed_instruction_length_decode(
xed_decoded_inst_zero_set_mode(&xedd, XED_MACHINE_MODE_LEGACY_32),
gc(unbingx86op(u"☼☼╚ª")), 4));
ASSERT_EQ(true, xedd.operands.amd3dnow);
ASSERT_EQ(0xa6, xedd.operands.nominal_opcode);
ASSERT_EQ(true, (int)xedd.op.amd3dnow);
ASSERT_EQ(0xa6, xedd.op.opcode);
}
TEST(x86ild, testPopToMemory) {
ASSERT_EQ(3, ild(u"ÅF◘")); /* 8f 46 08 */
}
TEST(x86ild, testFinit) {
ASSERT_EQ(1, ild(u"¢█π")); /* 9B DB E3: fwait */
ASSERT_EQ(2, ild(u"█π")); /* DB E3: fninit */
}
BENCH(x86ild, bench) {
uint8_t *x = gc(unbingx86op(u"b▓m◘Pî±   "));
struct XedDecodedInst xedd;
EZBENCH2("x86ild", donothing,
xed_instruction_length_decode(
xed_decoded_inst_zero_set_mode(&xedd, XED_MACHINE_MODE_LONG_64),
x, XED_MAX_INSTRUCTION_BYTES));
}

View file

@ -5,13 +5,15 @@ PKGS += TEST_NET_HTTP
TEST_NET_HTTP_SRCS := $(wildcard test/net/http/*.c)
TEST_NET_HTTP_SRCS_TEST = $(filter %_test.c,$(TEST_NET_HTTP_SRCS))
TEST_NET_HTTP_COMS = $(TEST_NET_HTTP_OBJS:%.o=%.com)
TEST_NET_HTTP_BINS = $(TEST_NET_HTTP_COMS) $(TEST_NET_HTTP_COMS:%=%.dbg)
TEST_NET_HTTP_OBJS = \
$(TEST_NET_HTTP_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_NET_HTTP_SRCS:%.c=o/$(MODE)/%.o)
TEST_NET_HTTP_COMS = \
$(TEST_NET_HTTP_SRCS:%.c=o/$(MODE)/%.com)
TEST_NET_HTTP_TESTS = \
$(TEST_NET_HTTP_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)

View file

@ -18,6 +18,7 @@
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/bits/initializer.h"
#include "libc/errno.h"
#include "libc/log/log.h"
#include "libc/macros.h"
@ -69,7 +70,7 @@ static struct UriMem {
struct UriKeyval params[4], queries[4];
} urimem_;
INITIALIZER(100, _init_uri, {
static textstartup void init() {
uri.segs.n = ARRAYLEN(urimem_.segs);
uri.segs.p = urimem_.segs;
uri.params.n = ARRAYLEN(urimem_.params);
@ -78,7 +79,9 @@ INITIALIZER(100, _init_uri, {
uri.queries.p = urimem_.queries;
uri.paramsegs.n = ARRAYLEN(urimem_.paramsegs);
uri.paramsegs.p = urimem_.paramsegs;
})
}
const void *const g_name_ctor[] initarray = {init};
TEST(uriparse, sipPstnUri) {
EXPECT_NE(-1, URIPARSE("sip:+12125650666"));

View file

@ -0,0 +1,106 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/assert.h"
#include "libc/limits.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/testlib.h"
#include "test/tool/build/lib/optest.h"
#include "tool/build/lib/alu.h"
#include "tool/build/lib/flags.h"
#define NATIVE_ALU2(MODE, INSTRUCTION) \
asm("pushf\n\t" \
"andl\t%3,(%%rsp)\n\t" \
"orl\t%4,(%%rsp)\n\t" \
"popf\n\t" INSTRUCTION "\t%" MODE "2,%" MODE "0\n\t" \
"pushf\n\t" \
"pop\t%q1" \
: "+r"(x), "=rm"(*f) \
: "r"(y), "i"(~FMASK), "r"(*f & FMASK) \
: "cc")
#define NATIVE_ALU2_ANYBITS(INSTRUCTION, MUTATING) \
switch (w) { \
case 0: \
NATIVE_ALU2("b", INSTRUCTION); \
if (MUTATING) x &= 0xff; \
return x; \
case 1: \
NATIVE_ALU2("w", INSTRUCTION); \
if (MUTATING) x &= 0xffff; \
return x; \
case 2: \
NATIVE_ALU2("k", INSTRUCTION); \
return x; \
case 3: \
NATIVE_ALU2("q", INSTRUCTION); \
return x; \
default: \
abort(); \
}
int64_t RunGolden(char w, int h, uint64_t x, uint64_t y, uint32_t *f) {
bool rw;
rw = !(h & ALU_TEST);
switch (h & 7) {
case ALU_OR:
NATIVE_ALU2_ANYBITS("or", rw);
case ALU_AND:
if (rw) {
NATIVE_ALU2_ANYBITS("and", rw);
} else {
NATIVE_ALU2_ANYBITS("test", rw);
}
case ALU_XOR:
NATIVE_ALU2_ANYBITS("xor", rw);
case ALU_SBB:
NATIVE_ALU2_ANYBITS("sbb", rw);
case ALU_CMP:
rw = false;
NATIVE_ALU2_ANYBITS("cmp", rw);
case ALU_SUB:
NATIVE_ALU2_ANYBITS("sub", rw);
case ALU_ADC:
NATIVE_ALU2_ANYBITS("adc", rw);
case ALU_ADD:
NATIVE_ALU2_ANYBITS("add", rw);
default:
abort();
}
}
const uint8_t kAluOps[] = {
ALU_ADD, ALU_OR, ALU_ADC, ALU_SBB, ALU_AND, ALU_SUB, ALU_XOR, ALU_CMP, ALU_AND | ALU_TEST,
};
const char *const kAluNames[] = {
[ALU_ADD] = "add", [ALU_OR] = "or", [ALU_ADC] = "adc",
[ALU_SBB] = "sbb", [ALU_AND] = "and", [ALU_SUB] = "sub",
[ALU_XOR] = "xor", [ALU_CMP] = "cmp", [ALU_AND | ALU_TEST] = "test",
};
int64_t RunOpTest(char w, int h, uint64_t x, uint64_t y, uint32_t *f) {
return Alu(w, h, x, y, f);
}
TEST(alu, test) {
RunOpTests(kAluOps, ARRAYLEN(kAluOps), kAluNames);
}

View file

@ -0,0 +1,112 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/macros.h"
#include "libc/testlib/testlib.h"
#include "test/tool/build/lib/numbers.h"
#include "tool/build/lib/bitscan.h"
#include "tool/build/lib/flags.h"
#define OSZ 00000000040
#define REXW 00000004000
struct Machine m[1];
struct XedDecodedInst xedd[1];
void SetUp(void) {
m->xedd = xedd;
}
TEST(bsr64, test) {
bool zf;
uint64_t i, w, x, a, b;
m->xedd->op.rde = REXW;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
a = AluBsr(m, 0, x);
asm("bsrq\t%2,%0" : "=r"(b), "=@ccz"(zf) : "r"(x));
ASSERT_EQ(zf, GetFlag(m->flags, FLAGS_ZF));
if (!zf) ASSERT_EQ(a, b);
}
}
TEST(bsr32, test) {
bool zf;
uint32_t i, w, x, a, b;
m->xedd->op.rde = 0;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
a = AluBsr(m, 0, x);
asm("bsrl\t%2,%0" : "=r"(b), "=@ccz"(zf) : "r"(x));
ASSERT_EQ(zf, GetFlag(m->flags, FLAGS_ZF));
if (!zf) ASSERT_EQ(a, b);
}
}
TEST(bsr16, test) {
bool zf;
uint16_t i, w, x, a, b;
m->xedd->op.rde = OSZ;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
a = AluBsr(m, 0, x);
asm("bsrw\t%2,%0" : "=r"(b), "=@ccz"(zf) : "r"(x));
ASSERT_EQ(zf, GetFlag(m->flags, FLAGS_ZF));
if (!zf) ASSERT_EQ(a, b);
}
}
TEST(bsf64, test) {
bool zf;
uint64_t i, w, x, a, b;
m->xedd->op.rde = REXW;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
a = AluBsf(m, 0, x);
asm("bsfq\t%2,%0" : "=r"(b), "=@ccz"(zf) : "r"(x));
ASSERT_EQ(zf, GetFlag(m->flags, FLAGS_ZF));
if (!zf) ASSERT_EQ(a, b);
}
}
TEST(bsf32, test) {
bool zf;
uint32_t i, w, x, a, b;
m->xedd->op.rde = 0;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
a = AluBsf(m, 0, x);
asm("bsfl\t%2,%0" : "=r"(b), "=@ccz"(zf) : "r"(x));
ASSERT_EQ(zf, GetFlag(m->flags, FLAGS_ZF));
if (!zf) ASSERT_EQ(a, b);
}
}
TEST(bsf16, test) {
bool zf;
uint16_t i, w, x, a, b;
m->xedd->op.rde = OSZ;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
a = AluBsf(m, 0, x);
asm("bsfw\t%2,%0" : "=r"(b), "=@ccz"(zf) : "r"(x));
ASSERT_EQ(zf, GetFlag(m->flags, FLAGS_ZF));
if (!zf) ASSERT_EQ(a, b, "%#lx", x);
}
}

View file

@ -0,0 +1,225 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/testlib.h"
#include "test/tool/build/lib/numbers.h"
#include "test/tool/build/lib/optest.h"
#include "tool/build/lib/alu.h"
#include "tool/build/lib/flags.h"
#include "tool/build/lib/machine.h"
#define NATIVE_ALU2(MODE, INSTRUCTION) \
asm("pushf\n\t" \
"andl\t%3,(%%rsp)\n\t" \
"orl\t%4,(%%rsp)\n\t" \
"popf\n\t" INSTRUCTION "\t%b2,%" MODE "0\n\t" \
"pushf\n\t" \
"pop\t%q1" \
: "+r"(x), "=r"(*f) \
: "c"(y), "i"(~FMASK), "r"(*f & FMASK) \
: "cc")
#define NATIVE_ALU2_ANYBITS(INSTRUCTION, MUTATING) \
switch (w) { \
case 0: \
NATIVE_ALU2("b", INSTRUCTION); \
if (MUTATING) x &= 0xff; \
return x; \
case 1: \
NATIVE_ALU2("w", INSTRUCTION); \
if (MUTATING) x &= 0xffff; \
return x; \
case 2: \
NATIVE_ALU2("k", INSTRUCTION); \
return x; \
case 3: \
NATIVE_ALU2("q", INSTRUCTION); \
return x; \
default: \
abort(); \
}
int64_t RunGolden(char w, int h, uint64_t x, uint64_t y, uint32_t *f) {
switch (h & 7) {
case BSU_ROR:
NATIVE_ALU2_ANYBITS("ror", true);
case BSU_SHL:
NATIVE_ALU2_ANYBITS("shl", true);
case BSU_SAL:
NATIVE_ALU2_ANYBITS("sal", true);
case BSU_RCR:
NATIVE_ALU2_ANYBITS("rcr", true);
case BSU_SAR:
NATIVE_ALU2_ANYBITS("sar", true);
case BSU_SHR:
NATIVE_ALU2_ANYBITS("shr", true);
case BSU_RCL:
NATIVE_ALU2_ANYBITS("rcl", true);
case BSU_ROL:
NATIVE_ALU2_ANYBITS("rol", true);
default:
abort();
}
}
const uint8_t kBsuOps[] = {
BSU_SHR, BSU_SHL, BSU_ROL, BSU_ROR, BSU_RCL,
BSU_RCR, BSU_SHL, BSU_SAL, BSU_SAR,
};
const char *const kBsuNames[] = {
[BSU_ROL] = "rol", [BSU_ROR] = "ror", [BSU_RCL] = "rcl", [BSU_RCR] = "rcr",
[BSU_SHL] = "shl", [BSU_SHR] = "shr", [BSU_SAL] = "sal", [BSU_SAR] = "sar",
};
int64_t RunOpTest(char w, int h, uint64_t x, uint64_t y, uint32_t *f) {
return Bsu(w, h, x, y, f);
}
void FixupUndefOpTestFlags(char w, int h, uint64_t x, uint64_t y,
uint32_t goldenflags, uint32_t *flags) {
y &= w == 3 ? 0x3F : 0x1F;
if (y != 0 && y != 1) {
*flags = SetFlag(*flags, FLAGS_OF, GetFlag(goldenflags, FLAGS_OF));
}
}
TEST(bsu, test) {
RunOpTests(kBsuOps, ARRAYLEN(kBsuOps), kBsuNames);
}
TEST(shrd32, smoke) {
int i, j, k;
uint8_t b, cf, of;
uint32_t x, y, z1, f, z2, unflag;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
for (j = 0; j < ARRAYLEN(kNumbers); ++j) {
y = kNumbers[j];
for (k = 0; k < ARRAYLEN(kNumbers); ++k) {
f = 0;
cf = 0;
of = 0;
b = kNumbers[k];
z1 = BsuDoubleShift(2, x, y, b, true, &f);
z2 = x;
asm("xor\t%1,%1\n\t"
"shrd\t%5,%4,%0"
: "+rm"(z2), "=&r"(unflag), "=@ccc"(cf), "=@cco"(of)
: "r"(y), "c"(b)
: "cc");
ASSERT_EQ(z2, z1);
ASSERT_EQ(cf, GetFlag(f, FLAGS_CF));
if (b <= 1) ASSERT_EQ(of, GetFlag(f, FLAGS_OF));
}
}
}
}
TEST(shld16, smoke) {
uint32_t f;
int i, j, k;
uint8_t b, cf, of;
uint16_t x, y, z1, z2, unflag;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
for (j = 0; j < ARRAYLEN(kNumbers); ++j) {
y = kNumbers[j];
for (k = 0; k < ARRAYLEN(kNumbers); ++k) {
f = 0;
cf = 0;
of = 0;
b = kNumbers[k];
if (b > 16) continue;
z1 = BsuDoubleShift(1, x, y, b, false, &f);
z2 = x;
asm("xor\t%1,%1\n\t"
"shldw\t%5,%4,%0"
: "+rm"(z2), "=&r"(unflag), "=@ccc"(cf), "=@cco"(of)
: "r"(y), "c"(b)
: "cc");
ASSERT_EQ(z2, z1);
ASSERT_EQ(cf, GetFlag(f, FLAGS_CF));
if (b <= 1) ASSERT_EQ(of, GetFlag(f, FLAGS_OF));
}
}
}
}
TEST(shld32, smoke) {
int i, j, k;
uint8_t b, cf, of;
uint32_t x, y, z1, f, z2, unflag;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
for (j = 0; j < ARRAYLEN(kNumbers); ++j) {
y = kNumbers[j];
for (k = 0; k < ARRAYLEN(kNumbers); ++k) {
f = 0;
cf = 0;
of = 0;
b = kNumbers[k];
if (b > 32) continue;
z1 = BsuDoubleShift(2, x, y, b, false, &f);
z2 = x;
asm("xor\t%1,%1\n\t"
"shld\t%5,%4,%0"
: "+rm"(z2), "=&r"(unflag), "=@ccc"(cf), "=@cco"(of)
: "r"(y), "c"(b)
: "cc");
ASSERT_EQ(z2, z1);
ASSERT_EQ(cf, GetFlag(f, FLAGS_CF));
if (b <= 1) ASSERT_EQ(of, GetFlag(f, FLAGS_OF));
}
}
}
}
TEST(shld64, smoke) {
uint32_t f;
int i, j, k;
uint8_t b, cf, of;
uint64_t x, y, z1, z2, unflag;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
for (j = 0; j < ARRAYLEN(kNumbers); ++j) {
y = kNumbers[j];
for (k = 0; k < ARRAYLEN(kNumbers); ++k) {
f = 0;
cf = 0;
of = 0;
b = kNumbers[k];
if (b > 64) continue;
z1 = BsuDoubleShift(3, x, y, b, false, &f);
z2 = x;
asm("xor\t%1,%1\n\t"
"shldq\t%5,%4,%0"
: "+rm"(z2), "=&r"(unflag), "=@ccc"(cf), "=@cco"(of)
: "r"(y), "c"(b)
: "cc");
ASSERT_EQ(z2, z1);
ASSERT_EQ(cf, GetFlag(f, FLAGS_CF));
if (b <= 1) ASSERT_EQ(of, GetFlag(f, FLAGS_OF));
}
}
}
}

View file

@ -0,0 +1,88 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/testlib/testlib.h"
#include "tool/build/lib/dis.h"
char b1[64];
char b2[64];
struct Dis d[1];
struct DisBuilder b = {d, d->xedd, 0};
TEST(DisInst, testInt3) {
uint8_t op[] = {0xcc};
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, op, sizeof(op)));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("int3 ", b1);
}
TEST(DisInst, testImmMem_needsSuffix) {
uint8_t op[] = {0x80, 0x3c, 0x07, 0x00};
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, op, sizeof(op)));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("cmpb $0,(%rdi,%rax)", b1);
}
TEST(DisInst, testImmReg_doesntNeedSuffix) {
uint8_t op[] = {0xb8, 0x08, 0x70, 0x40, 0x00};
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, op, sizeof(op)));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("mov $0x407008,%eax", b1);
}
TEST(DisInst, testPuttingOnTheRiz) {
static uint8_t ops[][15] = {
{0x8d, 0b00110100, 0b00100110},
{0x67, 0x8d, 0b00110100, 0b11100110},
{0x67, 0x8d, 0b10110100, 0b11100101, 0, 0, 0, 0},
{0x8d, 0b00110100, 0b11100101, 0x37, 0x13, 0x03, 0},
{0x8d, 0b10110100, 0b11100101, 0, 0, 0, 0},
};
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, ops[0], sizeof(ops[0])));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("lea (%rsi),%esi", b1);
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, ops[1], sizeof(ops[1])));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("lea (%esi,%eiz,8),%esi", b1);
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, ops[2], sizeof(ops[2])));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("lea 0(%ebp,%eiz,8),%esi", b1);
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, ops[3], sizeof(ops[3])));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("lea 0x31337,%esi", b1);
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, ops[4], sizeof(ops[4])));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("lea 0(%rbp,%riz,8),%esi", b1);
}
TEST(DisInst, testSibIndexOnly) {
uint8_t op[] = {76, 141, 4, 141, 0, 0, 0, 0}; /* lea 0x0(,%rcx,4),%r8 */
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, op, sizeof(op)));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("lea 0(,%rcx,4),%r8", b1);
}

View file

@ -0,0 +1,606 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/progn.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/log/check.h"
#include "libc/runtime/gc.h"
#include "libc/sysv/consts/sa.h"
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
#include "tool/build/lib/divmul.h"
#include "tool/build/lib/endian.h"
#include "tool/build/lib/flags.h"
#define CX 1
#define OSZ 00000000040
#define REXW 00000004000
#define RM(x) (0000000700 & ((x) << 006))
#define MOD(x) (0060000000 & ((x) << 026))
jmp_buf sigfpejmp;
struct Machine m[1];
struct sigaction oldsigfpe[1];
struct XedDecodedInst xedd[1];
void OnSigFpe(void) {
/* ProTip: gdb -ex 'handle SIGFPE nostop noprint pass' */
longjmp(sigfpejmp, 1);
}
void SetUp(void) {
m->xedd = xedd;
InitMachine(m);
CHECK_NE(-1, xsigaction(SIGFPE, OnSigFpe, SA_NODEFER, 0, oldsigfpe));
}
void TearDown(void) {
m->xedd = xedd;
CHECK_NE(-1, sigaction(SIGFPE, oldsigfpe, NULL));
}
TEST(imul8, test) {
static const uint8_t A[] = {0x00, 0x01, 0x80, 0x7F, 0x81, 0x7E, 0xFF, 0xBF};
int i, j;
int16_t ax;
bool cf, of;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write8(m->ax, A[i]);
Write8(m->cx, A[j]);
OpMulAxAlEbSigned(m);
asm volatile("imulb\t%3"
: "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "q"(A[j]), "0"(A[i])
: "cc");
EXPECT_EQ(ax, (int16_t)Read16(m->ax));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(imul16, test) {
static const uint16_t A[] = {0x0000, 0x0001, 0x8000, 0x7FFF, 0x8001, 0x7FFE,
0xFFFF, 0xBeef, 0x00b5, 0x00b6, 0xb504, 0xb505};
int i, j;
bool cf, of;
uint16_t dx, ax;
m->xedd->op.rde = OSZ | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write16(m->ax, A[i]);
Write16(m->cx, A[j]);
OpMulRdxRaxEvqpSigned(m);
asm("imulw\t%4"
: "=d"(dx), "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "r"(A[j]), "1"(A[i])
: "cc");
EXPECT_EQ((int32_t)((uint32_t)dx << 16 | ax),
(int32_t)((uint32_t)Read16(m->dx) << 16 | Read16(m->ax)));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(imul32, test) {
static const uint32_t A[] = {0x00000000, 0x00000001, 0x80000000, 0x7FFFFFFF,
0x80000001, 0x7FFFFFFE, 0xFFFFFFFF, 0xDeadBeef,
0x000000b6, 0x0000b504, 0x0000b505, 0xb504f334};
int i, j;
bool cf, of;
uint32_t dx, ax;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write32(m->ax, A[i]);
Write32(m->cx, A[j]);
OpMulRdxRaxEvqpSigned(m);
asm("imull\t%4"
: "=d"(dx), "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "r"(A[j]), "1"(A[i])
: "cc");
EXPECT_EQ((int64_t)((uint64_t)dx << 32 | ax),
(int64_t)((uint64_t)Read32(m->dx) << 32 | Read32(m->ax)));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(imul64, test) {
static const uint64_t A[] = {0x00000000, 0x00000001, 0x80000000, 0x7FFFFFFF,
0x80000001, 0x7FFFFFFE, 0xFFFFFFFF, 0xDeadBeef,
0x000000b6, 0x0000b504, 0x0000b505, 0xb504f334};
int i, j;
bool cf, of;
uint64_t dx, ax;
m->xedd->op.rde = REXW | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write64(m->ax, A[i]);
Write64(m->cx, A[j]);
OpMulRdxRaxEvqpSigned(m);
asm("imulq\t%4"
: "=d"(dx), "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "r"(A[j]), "1"(A[i])
: "cc");
EXPECT_EQ((int128_t)((uint128_t)dx << 64 | ax),
(int128_t)((uint128_t)Read64(m->dx) << 64 | Read64(m->ax)));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(mul8, test) {
static const uint8_t A[] = {0x00, 0x01, 0x80, 0x7F, 0x81, 0x7E, 0xFF, 0xb6};
int i, j;
uint16_t ax;
bool cf, of;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write8(m->ax, A[i]);
Write8(m->cx, A[j]);
OpMulAxAlEbUnsigned(m);
asm volatile("mulb\t%3"
: "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "q"(A[j]), "0"(A[i])
: "cc");
EXPECT_EQ(ax, Read16(m->ax));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(mul16, test) {
static const uint16_t A[] = {0x0000, 0x0001, 0x8000, 0x7FFF,
0x8001, 0x7FFE, 0xFFFF, 0x00b6};
int i, j;
bool cf, of;
uint16_t dx, ax;
m->xedd->op.rde = OSZ | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write16(m->ax, A[i]);
Write16(m->cx, A[j]);
OpMulRdxRaxEvqpUnsigned(m);
asm("mulw\t%4"
: "=d"(dx), "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "r"(A[j]), "1"(A[i])
: "cc");
EXPECT_EQ((uint32_t)((uint32_t)dx << 16 | ax),
(uint32_t)((uint32_t)Read16(m->dx) << 16 | Read16(m->ax)));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(mul32, test) {
static const uint32_t A[] = {0x00000000, 0x00000001, 0x80000000, 0x7FFFFFFF,
0x80000001, 0x7FFFFFFE, 0xFFFFFFFF, 0x000000b5,
0x000000b6, 0x0000b504, 0x0000b505, 0xb504f334};
int i, j;
bool cf, of;
uint32_t dx, ax;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write32(m->ax, A[i]);
Write32(m->cx, A[j]);
OpMulRdxRaxEvqpUnsigned(m);
asm("mull\t%4"
: "=d"(dx), "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "r"(A[j]), "1"(A[i])
: "cc");
EXPECT_EQ((uint64_t)((uint64_t)dx << 32 | ax),
(uint64_t)((uint64_t)Read32(m->dx) << 32 | Read32(m->ax)));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(mul64, test) {
static const uint64_t A[] = {0x00000000, 0x00000001, 0x80000000, 0x7FFFFFFF,
0x80000001, 0x7FFFFFFE, 0xFFFFFFFF, 0x000000b6,
0x0000b504, 0x0000b505, 0xb504f333, 0xb504f334};
int i, j;
bool cf, of;
uint64_t dx, ax;
m->xedd->op.rde = REXW | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write64(m->ax, A[i]);
Write64(m->cx, A[j]);
OpMulRdxRaxEvqpUnsigned(m);
asm("mulq\t%4"
: "=d"(dx), "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "r"(A[j]), "1"(A[i])
: "cc");
EXPECT_EQ((uint128_t)((uint128_t)dx << 64 | ax),
(uint128_t)((uint128_t)Read64(m->dx) << 64 | Read64(m->ax)));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(idiv8, test) {
static const uint8_t A[] = {0x00, 0x01, 0x80, 0x7F, 0x81, 0x7E, 0xFF, 0xBF};
uint16_t remquo;
bool gotthrow, gotsigfpe;
int8_t i, j, k, w, x, a, b;
int8_t quotient, remainder;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
m->ax[1] = A[i];
m->ax[0] = A[j];
m->cx[0] = A[k];
gotthrow = false;
gotsigfpe = false;
if (!setjmp(m->onhalt)) {
OpDivAlAhAxEbSigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
asm("idivb\t%1"
: "=a"(remquo)
: "q"(A[k]), "0"((int16_t)(A[i] << 8 | A[j]))
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
quotient = (int8_t)remquo;
remainder = (int8_t)(remquo >> 8);
EXPECT_EQ(quotient, (int8_t)m->ax[0]);
EXPECT_EQ(remainder, (int8_t)m->ax[1]);
}
}
}
}
}
TEST(idiv16, test) {
static const uint16_t A[] = {0x0000, 0x0001, 0x8000, 0x7FFF,
0x8001, 0x7FFE, 0xFFFF, 0xBeef};
bool gotthrow, gotsigfpe;
int16_t i, j, k, w, x, a, b;
int16_t quotient, remainder;
m->xedd->op.rde = OSZ | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
memcpy(m->dx, &A[i], 2);
memcpy(m->ax, &A[j], 2);
memcpy(m->cx, &A[k], 2);
if (!setjmp(m->onhalt)) {
gotthrow = false;
OpDivRdxRaxEvqpSigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
gotsigfpe = false;
asm("idivw\t%2"
: "=d"(remainder), "=a"(quotient)
: "r"(A[k]), "0"(A[i]), "1"(A[j])
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
EXPECT_EQ(quotient, (int16_t)Read16(m->ax));
EXPECT_EQ(remainder, (int16_t)Read16(m->dx));
}
}
}
}
}
TEST(idiv32, test) {
static const uint32_t A[] = {0x00000000, 0x00000001, 0x80000000, 0x7FFFFFFF,
0x80000001, 0x7FFFFFFE, 0xFFFFFFFF, 0xDeadBeef};
bool gotthrow, gotsigfpe;
int32_t i, j, k, w, x, a, b;
int32_t quotient, remainder;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
memcpy(m->dx, &A[i], 4);
memcpy(m->ax, &A[j], 4);
memcpy(m->cx, &A[k], 4);
if (!setjmp(m->onhalt)) {
gotthrow = false;
OpDivRdxRaxEvqpSigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
gotsigfpe = false;
asm("idivl\t%2"
: "=d"(remainder), "=a"(quotient)
: "r"(A[k]), "0"(A[i]), "1"(A[j])
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
EXPECT_EQ(quotient, (int32_t)Read32(m->ax));
EXPECT_EQ(remainder, (int32_t)Read32(m->dx));
}
}
}
}
}
TEST(idiv64, test) {
static const uint64_t A[] = {0x0000000000000000, 0x0000000000000001,
0x8000000000000000, 0x7FFFFFFFFFFFFFFF,
0x8000000000000001, 0x7FFFFFFFFFFFFFFE,
0xFFFFFFFFFFFFFFFF, 0x00DeadBeefCafe00};
bool gotthrow, gotsigfpe;
int64_t i, j, k, w, x, a, b;
int64_t quotient, remainder;
m->xedd->op.rde = REXW | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
memcpy(m->dx, &A[i], 8);
memcpy(m->ax, &A[j], 8);
memcpy(m->cx, &A[k], 8);
if (!setjmp(m->onhalt)) {
gotthrow = false;
OpDivRdxRaxEvqpSigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
gotsigfpe = false;
asm("idivq\t%2"
: "=d"(remainder), "=a"(quotient)
: "r"(A[k]), "0"(A[i]), "1"(A[j])
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
EXPECT_EQ(quotient, (int64_t)Read64(m->ax));
EXPECT_EQ(remainder, (int64_t)Read64(m->dx));
}
}
}
}
}
TEST(div, test) {
static const uint8_t A[] = {0x00, 0x01, 0x80, 0x7F, 0x81, 0x7E, 0xFF, 0xBF};
uint16_t remquo;
bool gotthrow, gotsigfpe;
uint8_t i, j, k, w, x, a, b;
uint8_t quotient, remainder;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
m->ax[1] = A[i];
m->ax[0] = A[j];
m->cx[0] = A[k];
gotthrow = false;
gotsigfpe = false;
if (!setjmp(m->onhalt)) {
OpDivAlAhAxEbUnsigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
asm("divb\t%1"
: "=a"(remquo)
: "q"(A[k]), "0"((uint16_t)(A[i] << 8 | A[j]))
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
quotient = (uint8_t)remquo;
remainder = (uint8_t)(remquo >> 8);
EXPECT_EQ(quotient, (uint8_t)m->ax[0]);
EXPECT_EQ(remainder, (uint8_t)m->ax[1]);
}
}
}
}
}
TEST(div16, test) {
static const uint16_t A[] = {0x0000, 0x0001, 0x8000, 0x7FFF,
0x8001, 0x7FFE, 0xFFFF, 0xBeef};
bool gotthrow, gotsigfpe;
uint16_t i, j, k, w, x, a, b;
uint16_t quotient, remainder;
m->xedd->op.rde = OSZ | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
memcpy(m->dx, &A[i], 2);
memcpy(m->ax, &A[j], 2);
memcpy(m->cx, &A[k], 2);
if (!setjmp(m->onhalt)) {
gotthrow = false;
OpDivRdxRaxEvqpUnsigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
gotsigfpe = false;
asm("divw\t%2"
: "=d"(remainder), "=a"(quotient)
: "r"(A[k]), "0"(A[i]), "1"(A[j])
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
EXPECT_EQ(quotient, (uint16_t)Read16(m->ax));
EXPECT_EQ(remainder, (uint16_t)Read16(m->dx));
}
}
}
}
}
TEST(div32, test) {
static const uint32_t A[] = {0x00000000, 0x00000001, 0x80000000, 0x7FFFFFFF,
0x80000001, 0x7FFFFFFE, 0xFFFFFFFF, 0xDeadBeef};
bool gotthrow, gotsigfpe;
uint32_t i, j, k, w, x, a, b;
uint32_t quotient, remainder;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
memcpy(m->dx, &A[i], 4);
memcpy(m->ax, &A[j], 4);
memcpy(m->cx, &A[k], 4);
if (!setjmp(m->onhalt)) {
gotthrow = false;
OpDivRdxRaxEvqpUnsigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
gotsigfpe = false;
asm("divl\t%2"
: "=d"(remainder), "=a"(quotient)
: "r"(A[k]), "0"(A[i]), "1"(A[j])
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
EXPECT_EQ(quotient, (uint32_t)Read32(m->ax));
EXPECT_EQ(remainder, (uint32_t)Read32(m->dx));
}
}
}
}
}
TEST(div64, test) {
static const uint64_t A[] = {0x0000000000000000, 0x0000000000000001,
0x8000000000000000, 0x7FFFFFFFFFFFFFFF,
0x8000000000000001, 0x7FFFFFFFFFFFFFFE,
0xFFFFFFFFFFFFFFFF, 0x00DeadBeefCafe00};
bool gotthrow, gotsigfpe;
uint64_t i, j, k, w, x, a, b;
uint64_t quotient, remainder;
m->xedd->op.rde = REXW | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
memcpy(m->dx, &A[i], 8);
memcpy(m->ax, &A[j], 8);
memcpy(m->cx, &A[k], 8);
if (!setjmp(m->onhalt)) {
gotthrow = false;
OpDivRdxRaxEvqpUnsigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
gotsigfpe = false;
asm("divq\t%2"
: "=d"(remainder), "=a"(quotient)
: "r"(A[k]), "0"(A[i]), "1"(A[j])
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
EXPECT_EQ(quotient, (uint64_t)Read64(m->ax));
EXPECT_EQ(remainder, (uint64_t)Read64(m->dx));
}
}
}
}
}
BENCH(imul, bench) {
volatile register int8_t x8, y8;
volatile register int16_t x16, y16;
volatile register int32_t x32, y32;
volatile register int64_t x64, y64;
EZBENCH2("imul8", PROGN(x8 = 7, y8 = 18), y8 *= x8);
EZBENCH2("imul16", PROGN(x16 = 123, y16 = 116), y16 *= x16);
EZBENCH2("imul32", PROGN(x32 = 0x238943, y32 = 0x238), y32 *= x32);
EZBENCH2("imul64", PROGN(x64 = 0x23894329838932, y64 = 0x238), y64 *= x64);
}
BENCH(idiv, bench) {
volatile register int8_t x8, y8;
volatile register int16_t x16, y16;
volatile register int32_t x32, y32;
volatile register int64_t x64, y64;
EZBENCH2("idiv8", PROGN(x8 = 7, y8 = 18), x8 /= y8);
EZBENCH2("idiv16", PROGN(x16 = 123, y16 = 116), x16 /= y16);
EZBENCH2("idiv32", PROGN(x32 = 0x238943298, y32 = 0x238), x32 /= y32);
EZBENCH2("idiv64", PROGN(x64 = 0x23894329838932, y64 = 0x238), x64 /= y64);
}
BENCH(mul, bench) {
volatile register uint8_t x8, y8;
volatile register uint16_t x16, y16;
volatile register uint32_t x32, y32;
volatile register uint64_t x64, y64;
EZBENCH2("mul8", PROGN(x8 = 7, y8 = 18), y8 *= x8);
EZBENCH2("mul16", PROGN(x16 = 123, y16 = 116), y16 *= x16);
EZBENCH2("mul32", PROGN(x32 = 0x238943, y32 = 0x238), y32 *= x32);
EZBENCH2("mul64", PROGN(x64 = 0x23894329838932, y64 = 0x238), y64 *= x64);
}
BENCH(div, bench) {
volatile register uint8_t x8, y8;
volatile register uint16_t x16, y16;
volatile register uint32_t x32, y32;
volatile register uint64_t x64, y64;
EZBENCH2("div8", PROGN(x8 = 7, y8 = 18), x8 /= y8);
EZBENCH2("div16", PROGN(x16 = 123, y16 = 116), x16 /= y16);
EZBENCH2("div32", PROGN(x32 = 0x238943298, y32 = 0x238), x32 /= y32);
EZBENCH2("div64", PROGN(x64 = 0x23894329838932, y64 = 0x238), x64 /= y64);
}

View file

@ -0,0 +1,298 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/progn.h"
#include "libc/fmt/bing.h"
#include "libc/math.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
#include "tool/build/lib/endian.h"
#include "tool/build/lib/fpu.h"
#include "tool/build/lib/machine.h"
#include "tool/build/lib/memory.h"
const uint8_t kPi80[] = {
0xd9, 0xe8, // fld1
0xb8, 0x0a, 0x00, 0x00, 0x00, // mov $0xa,%eax
0x31, 0xd2, // xor %edx,%edx
0xd9, 0xee, // fldz
0x48, 0x98, // cltq
0x48, 0x39, 0xc2, // cmp %rax,%rdx
0xd9, 0x05, 0x1a, 0x00, 0x00, 0x00, // flds 0x1a(%rip)
0x7d, 0x13, // jge 2b <pi80+0x2b>
0xde, 0xc1, // faddp
0x48, 0xff, 0xc2, // inc %rdx
0xd9, 0xfa, // fsqrt
0xd9, 0x05, 0x0f, 0x00, 0x00, 0x00, // flds 15(%rip)
0xd8, 0xc9, // fmul %st(1),%st
0xde, 0xca, // fmulp %st,%st(2)
0xeb, 0xe2, // jmp d <pi80+0xd>
0xdd, 0xd9, // fstp %st(1)
0xde, 0xf1, // fdivp
0xf4, // hlt
0x00, 0x00, 0x00, 0x40, // .float 2.0
0x00, 0x00, 0x00, 0x3f, // .float 0.5
};
const uint8_t kTenthprime[] = {
0x31, 0xd2, // xor %edx,%edx
0x45, 0x31, 0xc0, // xor %r8d,%r8d
0x31, 0xc9, // xor %ecx,%ecx
0xbe, 0x03, 0x00, 0x00, 0x00, // mov $0x3,%esi
0x41, 0xff, 0xc0, // inc %r8d
0x44, 0x89, 0xc0, // mov %r8d,%eax
0x83, 0xf9, 0x0a, // cmp $0xa,%ecx
0x74, 0x0b, // je 20
0x99, // cltd
0xf7, 0xfe, // idiv %esi
0x83, 0xfa, 0x01, // cmp $0x1,%edx
0x83, 0xd9, 0xff, // sbb $-1,%ecx
0xeb, 0xea, // jmp a
0xf4, // hlt
};
const uint8_t kTenthprime2[] = {
0xE8, 0x11, 0x00, 0x00, 0x00, //
0xF4, //
0x89, 0xF8, //
0xB9, 0x03, 0x00, 0x00, 0x00, //
0x99, //
0xF7, 0xF9, //
0x85, 0xD2, //
0x0F, 0x95, 0xC0, //
0xC3, //
0x55, //
0x48, 0x89, 0xE5, //
0x31, 0xF6, //
0x45, 0x31, 0xC0, //
0x44, 0x89, 0xC7, //
0xE8, 0xDF, 0xFF, 0xFF, 0xFF, //
0x0F, 0xB6, 0xC0, //
0x66, 0x83, 0xF8, 0x01, //
0x83, 0xDE, 0xFF, //
0x41, 0xFF, 0xC0, //
0x83, 0xFE, 0x0A, //
0x75, 0xE6, //
0x44, 0x89, 0xC0, //
0x5D, //
0xC3, //
};
int64_t base;
uint8_t *real;
size_t realsize;
struct Machine *m;
void SetUp(void) {
base = 0;
m = NewMachine();
realsize = 0x10000;
real = tmemalign(PAGESIZE, ROUNDUP(realsize, PAGESIZE));
RegisterMemory(m, base, real, realsize);
m->ip = base;
Write64(m->sp, m->ip + realsize);
}
void TearDown(void) {
ResetRam(m);
tfree(real);
free(m);
}
int ExecuteUntilHalt(struct Machine *m) {
int rc;
if (!(rc = setjmp(m->onhalt))) {
for (;;) {
LoadInstruction(m);
ExecuteInstruction(m);
}
} else {
return rc;
}
}
TEST(machine, test) {
memcpy(real, kTenthprime, sizeof(kTenthprime));
ASSERT_EQ(kMachineHalt, ExecuteUntilHalt(m));
ASSERT_EQ(15, Read32(m->ax));
}
TEST(machine, testFpu) {
memcpy(real, kPi80, sizeof(kPi80));
ASSERT_EQ(kMachineHalt, ExecuteUntilHalt(m));
ASSERT_TRUE(fabs(3.14159 - FpuPop(m)) < 0.0001);
m->ip = base;
ASSERT_EQ(kMachineHalt, ExecuteUntilHalt(m));
ASSERT_TRUE(fabs(3.14159 - FpuPop(m)) < 0.0001);
}
BENCH(machine, benchPrimeNumberPrograms) {
memcpy(real, kTenthprime2, sizeof(kTenthprime2));
EZBENCH2("tenthprime2", m->ip = base, ExecuteUntilHalt(m));
ASSERT_EQ(15, Read32(m->ax));
memcpy(real, kTenthprime, sizeof(kTenthprime));
EZBENCH2("tenthprime", m->ip = base, ExecuteUntilHalt(m));
ASSERT_EQ(15, Read32(m->ax));
}
BENCH(machine, benchFpu) {
memcpy(real, kPi80, sizeof(kPi80));
EZBENCH2("pi80", m->ip = base, PROGN(ExecuteUntilHalt(m), FpuPop(m)));
}
BENCH(machine, benchLoadExec2) {
uint8_t kMovCode[] = {0xbe, 0x03, 0x00, 0x00, 0x00};
memcpy(real, kMovCode, sizeof(kMovCode));
LoadInstruction(m);
EZBENCH2("mov", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchLoadExec3) {
uint8_t kMovdCode[] = {0x66, 0x0f, 0x6e, 0xc0};
Write64(m->ax, 0);
memcpy(real, kMovdCode, sizeof(kMovdCode));
LoadInstruction(m);
EZBENCH2("movd", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchLoadExec4) {
uint8_t kAddpsRegregCode[] = {0x0f, 0x58, 0xC0};
uint8_t kAddpsMemregCode[] = {0x0f, 0x58, 0x00};
Write64(m->ax, 0);
memcpy(real, kAddpsRegregCode, sizeof(kAddpsRegregCode));
LoadInstruction(m);
EZBENCH2("addps reg reg", m->ip = base, ExecuteInstruction(m));
memcpy(real, kAddpsMemregCode, sizeof(kAddpsMemregCode));
LoadInstruction(m);
EZBENCH2("addps mem reg", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchLoadExec5) {
uint8_t kPaddwRegregCode[] = {0x66, 0x0F, 0xFD, 0xC0};
uint8_t kPaddwMemregCode[] = {0x66, 0x0F, 0xFD, 0x00};
Write64(m->ax, 0);
memcpy(real, kPaddwRegregCode, sizeof(kPaddwRegregCode));
LoadInstruction(m);
EZBENCH2("paddw", m->ip = base, ExecuteInstruction(m));
memcpy(real, kPaddwMemregCode, sizeof(kPaddwMemregCode));
LoadInstruction(m);
EZBENCH2("paddw mem", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchLoadExec6) {
uint8_t kPsubqRegregCode[] = {0x66, 0x0F, 0xFB, 0xC0};
uint8_t kPsubqMemregCode[] = {0x66, 0x0F, 0xFB, 0x00};
Write64(m->ax, 0);
memcpy(real, kPsubqRegregCode, sizeof(kPsubqRegregCode));
LoadInstruction(m);
EZBENCH2("psubq", m->ip = base, ExecuteInstruction(m));
memcpy(real, kPsubqMemregCode, sizeof(kPsubqMemregCode));
LoadInstruction(m);
EZBENCH2("psubq mem", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchAddqMem) {
uint8_t kAddMemregCode[] = {0x48, 0x03, 0x08};
Write64(m->ax, 0);
memcpy(real, kAddMemregCode, sizeof(kAddMemregCode));
LoadInstruction(m);
EZBENCH2("addq mem", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchAddlMem) {
uint8_t kAddMemregCode[] = {0x03, 0x08};
Write64(m->ax, 0);
memcpy(real, kAddMemregCode, sizeof(kAddMemregCode));
LoadInstruction(m);
EZBENCH2("addl mem", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchAddq) {
uint8_t kAddqCode[] = {0x48, 0x01, 0xd8};
Write64(m->ax, 0);
memcpy(real, kAddqCode, sizeof(kAddqCode));
LoadInstruction(m);
EZBENCH2("addq", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchAddb) {
uint8_t kAddbCode[] = {0x00, 0xd8};
Write64(m->ax, 0);
memcpy(real, kAddbCode, sizeof(kAddbCode));
LoadInstruction(m);
EZBENCH2("addb", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchXorReg) {
memcpy(real, kTenthprime, sizeof(kTenthprime));
LoadInstruction(m);
EZBENCH2("xor", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchLoadExec8) {
uint8_t kFchsCode[] = {0xd9, 0xe0};
Write64(m->ax, 0);
OpFinit(m);
*FpuSt(m, 0) = M_PI;
FpuSetTag(m, 0, kFpuTagValid);
memcpy(real, kFchsCode, sizeof(kFchsCode));
LoadInstruction(m);
EZBENCH2("fchs", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchPushpop) {
uint8_t kPushpop[] = {0x50, 0x58};
Write64(m->ax, 0);
memcpy(real, kPushpop, sizeof(kPushpop));
EZBENCH2("pushpop", m->ip = base,
PROGN(LoadInstruction(m), ExecuteInstruction(m), LoadInstruction(m),
ExecuteInstruction(m)));
}
BENCH(machine, benchPause) {
uint8_t kPause[] = {0xf3, 0x90};
Write64(m->ax, 0);
memcpy(real, kPause, sizeof(kPause));
LoadInstruction(m);
EZBENCH2("pause", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchClc) {
uint8_t kClc[] = {0xf8};
Write64(m->ax, 0);
memcpy(real, kClc, sizeof(kClc));
LoadInstruction(m);
EZBENCH2("clc", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchNop) {
uint8_t kNop[] = {0x90};
Write64(m->ax, 0);
memcpy(real, kNop, sizeof(kNop));
LoadInstruction(m);
EZBENCH2("nop", m->ip = base, ExecuteInstruction(m));
}
TEST(machine, sizeIsReasonable) {
ASSERT_LE(sizeof(struct Machine), 65536);
}

View file

@ -0,0 +1,105 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/mem/mem.h"
#include "libc/runtime/gc.h"
#include "libc/testlib/testlib.h"
#include "third_party/xed/x86.h"
#include "tool/build/lib/endian.h"
#include "tool/build/lib/machine.h"
#include "tool/build/lib/modrm.h"
TEST(modrm, testAddressSizeOverride_isNotPresent_keepsWholeExpression) {
struct Machine *m = gc(NewMachine());
struct XedDecodedInst *xedd = gc(calloc(1, sizeof(struct XedDecodedInst)));
uint8_t op[] = {0x8d, 0x04, 0x03}; /* lea (%rbx,%rax,1),%eax */
m->xedd = xedd;
Write64(m->bx, 0x2);
Write64(m->ax, 0xffffffff);
xed_decoded_inst_zero_set_mode(xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(xedd, op, sizeof(op)));
EXPECT_EQ(0x100000001, ComputeAddress(m));
}
TEST(modrm, testAddressSizeOverride_isPresent_modulesWholeExpression) {
struct Machine *m = gc(NewMachine());
struct XedDecodedInst *xedd = gc(calloc(1, sizeof(struct XedDecodedInst)));
uint8_t op[] = {0x67, 0x8d, 0x04, 0x03}; /* lea (%ebx,%eax,1),%eax */
m->xedd = xedd;
Write64(m->bx, 0x2);
Write64(m->ax, 0xffffffff);
xed_decoded_inst_zero_set_mode(xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(xedd, op, sizeof(op)));
EXPECT_EQ(0x000000001, ComputeAddress(m));
}
TEST(modrm, testOverflow_doesntTriggerTooling) {
struct Machine *m = gc(NewMachine());
struct XedDecodedInst *xedd = gc(calloc(1, sizeof(struct XedDecodedInst)));
uint8_t op[] = {0x8d, 0x04, 0x03}; /* lea (%rbx,%rax,1),%eax */
m->xedd = xedd;
Write64(m->bx, 0x0000000000000001);
Write64(m->ax, 0x7fffffffffffffff);
xed_decoded_inst_zero_set_mode(xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(xedd, op, sizeof(op)));
EXPECT_EQ(0x8000000000000000ull, (uint64_t)ComputeAddress(m));
}
TEST(modrm, testPuttingOnTheRiz) {
struct Machine *m = gc(NewMachine());
static uint8_t ops[][15] = {
{0x8d, 0b00110100, 0b00100110}, // lea (%rsi),%esi
{0x67, 0x8d, 0b00110100, 0b11100110}, // lea (%esi,%eiz,8),%esi
{103, 141, 180, 229, 55, 19, 3, 0}, // lea 0x31337(%ebp,%eiz,8),%esi
{141, 52, 229, 55, 19, 3, 0}, // lea 0x31337,%esi
};
m->xedd = gc(calloc(1, sizeof(struct XedDecodedInst)));
Write64(m->si, 0x100000001);
Write64(m->bp, 0x200000002);
xed_decoded_inst_zero_set_mode(m->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(m->xedd, ops[0], sizeof(ops[0])));
EXPECT_EQ(0x100000001, ComputeAddress(m));
xed_decoded_inst_zero_set_mode(m->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(m->xedd, ops[1], sizeof(ops[1])));
EXPECT_EQ(0x000000001, ComputeAddress(m));
xed_decoded_inst_zero_set_mode(m->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(m->xedd, ops[2], sizeof(ops[2])));
EXPECT_EQ(0x31339, ComputeAddress(m));
xed_decoded_inst_zero_set_mode(m->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(m->xedd, ops[3], sizeof(ops[3])));
EXPECT_EQ(0x31337, ComputeAddress(m));
}
TEST(modrm, testSibIndexOnly) {
// mod = 0b00 (0)
// reg = 0b000 (0)
// rm = 0b100 (4)
// scale = 0b10 (2)
// index = 0b001 (1)
// base = 0b101 (5)
struct Machine *m = gc(NewMachine());
struct XedDecodedInst *xedd = gc(calloc(1, sizeof(struct XedDecodedInst)));
uint8_t op[] = {76, 141, 4, 141, 0, 0, 0, 0}; /* lea 0x0(,%rcx,4),%r8 */
m->xedd = xedd;
Write64(m->bp, 0x123);
Write64(m->cx, 0x123);
xed_decoded_inst_zero_set_mode(xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(xedd, op, sizeof(op)));
EXPECT_EQ(0x123 * 4, (uint64_t)ComputeAddress(m));
}

View file

@ -0,0 +1,57 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "test/tool/build/lib/numbers.h"
const uint64_t kNumbers[102] = {
0x0000000000000000, 0x0000000080000000, 0x0000000000008000,
0x0000000000000001, 0x0000000000000002, 0x0000000000000003,
0x0000000000000004, 0x0000000000000005, 0x0000000000000006,
0x0000000000000007, 0x0000000000000008, 0x0000000000000009,
0x000000000000000a, 0x000000000000000b, 0x000000000000000c,
0x000000000000000d, 0x000000000000000e, 0x000000000000000f,
0x0000000000000010, 0x0000000000000011, 0x0000000000000012,
0x0000000000000013, 0x0000000000000014, 0x0000000000000015,
0x0000000000000016, 0x0000000000000017, 0x0000000000000018,
0x0000000000000019, 0x000000000000001a, 0x000000000000001b,
0x000000000000001c, 0x000000000000001d, 0x000000000000001e,
0x000000000000001f, 0x0000000000000020, 0x0000000000000040,
0x8000000000000000, 0x0000000000000080, 0x0000000000000002,
0x0000000000000001, 0x0000000000000004, 0x00000000C0000000,
0xC000000000000000, 0x000000000000C000, 0x00000000000000C0,
0x0000000000000003, 0x000000000000E000, 0x00000000E0000000,
0xE000000000000000, 0x00000000000000E0, 0x000000000000001F,
0x00000000000000FC, 0x000000000000003F, 0x000000000000007F,
0x00000000000000FB, 0x00000000000000FD, 0x00000000000000FE,
0x00000000000000FF, 0x000000000000FF1F, 0x0000000000001FFF,
0x000000000000FFFC, 0x0000000000003FFF, 0x000000000000FF3F,
0x000000000000FFFD, 0x000000000000FFFE, 0x000000000000FFFB,
0x000000000000FF7F, 0x0000000000007FFF, 0x000000000000FFFF,
0x00000000FFFF1FFF, 0x00000000FFFFFF1F, 0x000000001FFFFFFF,
0x00000000FFFFFF3F, 0x00000000FFFF3FFF, 0x00000000FFFFFFFC,
0x000000003FFFFFFF, 0x00000000FFFFFF7F, 0x00000000FFFFFFFD,
0x00000000FFFFFFFE, 0x00000000FFFFFFFB, 0x000000007FFFFFFF,
0x00000000FFFF7FFF, 0x00000000FFFFFFFF, 0xFFFFFFFF1FFFFFFF,
0x1FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFF1FFF, 0xFFFFFFFFFFFFFF1F,
0xFFFFFFFFFFFFFFFC, 0xFFFFFFFFFFFF3FFF, 0xFFFFFFFF3FFFFFFF,
0xFFFFFFFFFFFFFF3F, 0x3FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFD,
0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFB, 0xFFFFFFFF7FFFFFFF,
0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFF7F, 0xFFFFFFFFFFFF7FFF,
0xFFFFFFFFFFFFFFFF, 0x00DeadBeefCafe00, 0x0000031337000000,
};

View file

@ -0,0 +1,10 @@
#ifndef COSMOPOLITAN_TEST_TOOL_BUILD_LIB_NUMBERS_H_
#define COSMOPOLITAN_TEST_TOOL_BUILD_LIB_NUMBERS_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
extern const uint64_t kNumbers[102];
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_TEST_TOOL_BUILD_LIB_NUMBERS_H_ */

View file

@ -0,0 +1,94 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/weaken.h"
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "test/tool/build/lib/numbers.h"
#include "test/tool/build/lib/optest.h"
#include "tool/build/lib/flags.h"
const char kOpSuffix[] = {'b', 'w', 'l', 'q'};
void(RunOpTests)(const uint8_t *ops, size_t n, const char *const *opnames,
const char *file, int line, const char *func) {
uint64_t x, y;
uint64_t xn, xp;
uint32_t f0, f1, f2;
long failed, succeeded;
int w, h, i, j, c, z, s, o, p;
failed = 0;
succeeded = 0;
for (w = 0; w < 4; ++w) {
for (h = 0; h < n; ++h) {
for (z = 0; z < 2; ++z) {
for (o = 0; o < 2; ++o) {
for (s = 0; s < 2; ++s) {
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
for (j = 0; j < ARRAYLEN(kNumbers); ++j) {
for (c = 0; c < 2; ++c) {
x = kNumbers[i];
y = kNumbers[j];
f2 = f1 = f0 = c << FLAGS_CF | z << FLAGS_ZF | s << FLAGS_SF |
o << FLAGS_OF;
xn = RunGolden(w, ops[h], x, y, &f1);
xp = RunOpTest(w, ops[h], x, y, &f2);
if (weaken(FixupUndefOpTestFlags)) {
FixupUndefOpTestFlags(w, ops[h], x, y, f1, &f2);
}
if (xn == xp && (f1 & FMASK) == (f2 & FMASK)) {
succeeded++;
} else if (failed++ < 10) {
fprintf(stderr,
"%s:%d:%s: %s%c failed\n\t"
"𝑥 %016x\n\t"
"𝑦 %016x %c%c%c%c 0NPlODITSZKA1PVC\n\t"
"𝑥ₙ %016x %c%c%c%c %016b\n\t"
"𝑥ₚ %016x %c%c%c%c %016b\n",
file, line, func, opnames[ops[h]], kOpSuffix[w], x,
y, ".C"[c], ".Z"[z], ".S"[s], ".O"[o], xn,
".C"[!!(f1 & (1 << FLAGS_CF))],
".Z"[!!(f1 & (1 << FLAGS_ZF))],
".S"[!!(f1 & (1 << FLAGS_SF))],
".O"[!!(f1 & (1 << FLAGS_OF))], f1, xp,
".C"[!!(f2 & (1 << FLAGS_CF))],
".Z"[!!(f2 & (1 << FLAGS_ZF))],
".S"[!!(f2 & (1 << FLAGS_SF))],
".O"[!!(f2 & (1 << FLAGS_OF))], f2);
}
}
}
}
}
}
}
}
}
if (failed) {
fprintf(stderr,
"\n"
"passing: %d%%\n"
"succeeded: %,ld\n"
"failed: %,ld\n"
"\n",
(int)((1 - (double)failed / succeeded) * 100), succeeded, failed);
exit(1);
}
}

View file

@ -0,0 +1,21 @@
#ifndef COSMOPOLITAN_TEST_TOOL_BUILD_LIB_OPTEST_H_
#define COSMOPOLITAN_TEST_TOOL_BUILD_LIB_OPTEST_H_
#include "tool/build/lib/flags.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define FMASK (1 << FLAGS_CF | 1 << FLAGS_ZF | 1 << FLAGS_SF | 1 << FLAGS_OF)
void RunOpTests(const uint8_t *, size_t, const char *const *, const char *, int,
const char *);
int64_t RunGolden(char, int, uint64_t, uint64_t, uint32_t *);
int64_t RunOpTest(char, int, uint64_t, uint64_t, uint32_t *);
void FixupUndefOpTestFlags(char, int, uint64_t, uint64_t, uint32_t, uint32_t *);
#define RunOpTests(ops, n, names) \
RunOpTests(ops, n, names, __FILE__, __LINE__, __func__)
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_TEST_TOOL_BUILD_LIB_OPTEST_H_ */

View file

@ -0,0 +1,150 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/log/check.h"
#include "libc/macros.h"
#include "libc/mem/mem.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "tool/build/lib/memory.h"
#include "tool/build/lib/pml4t.h"
struct Unmapped {
size_t i;
struct UnmappedEntry {
void *addr;
size_t size;
} p[8];
};
struct Unmapped unmapped;
uint64_t *pt2, *pt3, *pt4;
pml4t_t cr3;
void *NewPage(void) {
void *p;
p = tmemalign(4096, 4096);
CHECK_ALIGNED(4096, p);
memset(p, 0, 4096);
return p;
}
void FreePage(void *p) {
tfree(p);
}
int FakeMunmap(void *addr, size_t size) {
return 0;
}
int MockMunmap(void *addr, size_t size) {
CHECK_LT(unmapped.i, ARRAYLEN(unmapped.p));
unmapped.p[unmapped.i].addr = addr;
unmapped.p[unmapped.i].size = size;
unmapped.i++;
return 0;
}
void SetUp(void) {
memset(cr3, 0, sizeof(cr3));
}
void TearDown(void) {
unmapped.i = 0;
FreePml4t(cr3, -0x800000000000, 0x800000000000, FreePage, FakeMunmap);
}
TEST(pml4t, testHighestAddress) {
ASSERT_NE(-1,
RegisterPml4t(cr3, 0x7fffffffe000, 0x3133700000, 0x2000, NewPage));
ASSERT_TRUE(IsValidPage(cr3[255]));
pt2 = UnmaskPageAddr(cr3[255]);
ASSERT_TRUE(IsValidPage(pt2[511]));
pt3 = UnmaskPageAddr(pt2[511]);
ASSERT_TRUE(IsValidPage(pt3[511]));
pt4 = UnmaskPageAddr(pt3[511]);
ASSERT_TRUE(IsValidPage(pt4[510]));
ASSERT_TRUE(IsValidPage(pt4[511]));
EXPECT_EQ(0x3133700000, UnmaskPageAddr(pt4[510]));
EXPECT_EQ(0x3133701000, UnmaskPageAddr(pt4[511]));
}
TEST(pml4t, testLowestAddress) {
ASSERT_NE(-1,
RegisterPml4t(cr3, -0x800000000000, 0x31337000, 0x2000, NewPage));
ASSERT_TRUE(IsValidPage(cr3[256]));
pt2 = UnmaskPageAddr(cr3[256]);
ASSERT_TRUE(IsValidPage(pt2[0]));
pt3 = UnmaskPageAddr(pt2[0]);
ASSERT_TRUE(IsValidPage(pt3[0]));
pt4 = UnmaskPageAddr(pt3[0]);
ASSERT_TRUE(IsValidPage(pt4[0]));
ASSERT_TRUE(IsValidPage(pt4[1]));
}
TEST(pml4t, testOverlapsExistingRegistration_overwritesRegistration) {
ASSERT_NE(-1,
RegisterPml4t(cr3, 0x7fffffffe000, 0x31337000, 0x1000, NewPage));
ASSERT_TRUE(IsValidPage(cr3[255]));
pt2 = UnmaskPageAddr(cr3[255]);
ASSERT_TRUE(IsValidPage(pt2[511]));
pt3 = UnmaskPageAddr(pt2[511]);
ASSERT_TRUE(IsValidPage(pt3[511]));
pt4 = UnmaskPageAddr(pt3[511]);
EXPECT_TRUE(IsValidPage(pt4[510]));
EXPECT_EQ(0x31337000, UnmaskPageAddr(pt4[510]));
ASSERT_NE(-1, RegisterPml4t(cr3, 0x7fffffffe000, 0x31337000 + 0x1000, 0x1000,
NewPage));
EXPECT_TRUE(IsValidPage(pt4[510]));
EXPECT_EQ(0x31337000 + 0x1000, UnmaskPageAddr(pt4[510]));
}
TEST(pml4t, testFindPml4t_holeTooSmall_skipsOver) {
ASSERT_NE(-1, RegisterPml4t(cr3, 0x700000000, 0, 0x1000, NewPage));
ASSERT_NE(-1, RegisterPml4t(cr3, 0x700005000, 0, 0x1000, NewPage));
ASSERT_EQ(0x700001000, FindPml4t(cr3, 0x700000000, 0x01000, NewPage));
ASSERT_EQ(0x700006000, FindPml4t(cr3, 0x700000000, 0x10000, NewPage));
}
TEST(pml4t, testFreePmlt) {
ASSERT_NE(-1, RegisterPml4t(cr3, 0x000005000, 0x123000, 0x2000, NewPage));
ASSERT_NE(-1, RegisterPml4t(cr3, 0x000000000, 0x321000, 0x1000, NewPage));
ASSERT_NE(-1, FreePml4t(cr3, -0x800000000000, 0x800000000000, FreePage,
MockMunmap));
ASSERT_EQ(2, unmapped.i);
EXPECT_EQ(0x321000, unmapped.p[0].addr);
EXPECT_EQ(0x1000, unmapped.p[0].size);
EXPECT_EQ(0x123000, unmapped.p[1].addr);
EXPECT_EQ(0x2000, unmapped.p[1].size);
}
BENCH(pml4t, bench) {
EZBENCH2("RegisterPml4t 1mb fresh",
FreePml4t(cr3, -0x800000000000, 0x800000000000, free, FakeMunmap),
RegisterPml4t(cr3, 0x00100000, 0x31337000, 0x00100000, MallocPage));
EZBENCH2("RegisterPml4t 1gb fresh",
FreePml4t(cr3, -0x800000000000, 0x800000000000, free, FakeMunmap),
RegisterPml4t(cr3, 0x40000000, 0x31337000, 0x40000000, MallocPage));
EZBENCH2("RegisterPml4t 1mb overwrite", donothing,
RegisterPml4t(cr3, 0x00100000, 0x31337000, 0x00100000, MallocPage));
EZBENCH2("RegisterPml4t 1gb overwrite", donothing,
RegisterPml4t(cr3, 0x40000000, 0x31337000, 0x40000000, MallocPage));
FreePml4t(cr3, -0x800000000000, 0x800000000000, free, FakeMunmap);
}

View file

@ -3,14 +3,21 @@
PKGS += TEST_TOOL_BUILD_LIB
TEST_TOOL_BUILD_LIB_SRCS := $(wildcard test/tool/build/lib/*.c)
TEST_TOOL_BUILD_LIB = $(TOOL_BUILD_LIB_A_DEPS) $(TOOL_BUILD_LIB_A)
TEST_TOOL_BUILD_LIB_A = o/$(MODE)/test/tool/build/lib/buildlib.a
TEST_TOOL_BUILD_LIB_FILES := $(wildcard test/tool/build/lib/*)
TEST_TOOL_BUILD_LIB_SRCS = $(filter %.c,$(TEST_TOOL_BUILD_LIB_FILES))
TEST_TOOL_BUILD_LIB_SRCS_TEST = $(filter %_test.c,$(TEST_TOOL_BUILD_LIB_SRCS))
TEST_TOOL_BUILD_LIB_HDRS = $(filter %.h,$(TEST_TOOL_BUILD_LIB_FILES))
TEST_TOOL_BUILD_LIB_COMS = $(TEST_TOOL_BUILD_LIB_OBJS:%.o=%.com)
TEST_TOOL_BUILD_LIB_OBJS = \
$(TEST_TOOL_BUILD_LIB_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_TOOL_BUILD_LIB_SRCS:%.c=o/$(MODE)/%.o)
TEST_TOOL_BUILD_LIB_COMS = \
$(TEST_TOOL_BUILD_LIB_SRCS:%.c=o/$(MODE)/%.com)
TEST_TOOL_BUILD_LIB_BINS = \
$(TEST_TOOL_BUILD_LIB_COMS) \
$(TEST_TOOL_BUILD_LIB_COMS:%=%.dbg)
@ -19,34 +26,49 @@ TEST_TOOL_BUILD_LIB_TESTS = \
$(TEST_TOOL_BUILD_LIB_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
TEST_TOOL_BUILD_LIB_CHECKS = \
$(TEST_TOOL_BUILD_LIB_HDRS:%=o/$(MODE)/%.ok) \
$(TEST_TOOL_BUILD_LIB_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
TEST_TOOL_BUILD_LIB_DIRECTDEPS = \
LIBC_X \
LIBC_CALLS \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_RUNTIME \
LIBC_FMT \
LIBC_STDIO \
LIBC_LOG \
LIBC_SYSV \
LIBC_STUBS \
LIBC_UNICODE \
LIBC_TESTLIB \
TOOL_BUILD_LIB
TOOL_BUILD_LIB \
THIRD_PARTY_COMPILER_RT \
THIRD_PARTY_XED
TEST_TOOL_BUILD_LIB_DEPS := \
$(call uniq,$(foreach x,$(TEST_TOOL_BUILD_LIB_DIRECTDEPS),$($(x))))
o/$(MODE)/test/tool/build/lib/buildlib.pkg: \
$(TEST_TOOL_BUILD_LIB_A): \
test/tool/build/lib/ \
$(TEST_TOOL_BUILD_LIB_A).pkg \
$(TEST_TOOL_BUILD_LIB_OBJS)
$(TEST_TOOL_BUILD_LIB_A).pkg: \
$(TEST_TOOL_BUILD_LIB_OBJS) \
$(foreach x,$(TEST_TOOL_BUILD_LIB_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/test/tool/build/lib/%.com.dbg: \
$(TEST_TOOL_BUILD_LIB_DEPS) \
$(TEST_TOOL_BUILD_LIB_A) \
o/$(MODE)/test/tool/build/lib/%.o \
o/$(MODE)/test/tool/build/lib/buildlib.pkg \
$(TEST_TOOL_BUILD_LIB_A).pkg \
$(LIBC_TESTMAIN) \
$(CRT) \
$(APE)
@$(APELINK)
.PHONY: o/$(MODE)/test/tool/build/lib
.PHONY: o/$(MODE)/test/tool/build/lib
o/$(MODE)/test/tool/build/lib: \
$(TEST_TOOL_BUILD_LIB_BINS) \
$(TEST_TOOL_BUILD_LIB_CHECKS)

View file

@ -17,39 +17,23 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/intrin/paddsw.h"
#include "libc/intrin/paddw.h"
#include "libc/limits.h"
#include "libc/testlib/testlib.h"
#include "tool/build/lib/x87.h"
TEST(paddw, test) {
short A[8] = {7};
short B[8] = {11};
short C[8];
paddw(C, A, B);
EXPECT_EQ(18, C[0]);
TEST(x87, fprem) {
ASSERT_LDBL_EQ(1, fprem(1, -1.5, NULL));
ASSERT_LDBL_EQ(1.1766221079117338e-14L,
fprem(12300000000000000.L, .0000000000000123L, NULL));
}
TEST(paddsw, test) {
short A[8] = {7};
short B[8] = {11};
short C[8];
paddsw(C, A, B);
EXPECT_EQ(18, C[0]);
TEST(x87, fprem1) {
ASSERT_LDBL_EQ(-.5, fprem1(1, -1.5, NULL));
ASSERT_LDBL_EQ(-5.337789208826618e-16,
fprem1(12300000000000000.L, .0000000000000123L, NULL));
}
TEST(paddw, testOverflow_wrapsAround) {
short A[8] = {SHRT_MAX, SHRT_MIN};
short B[8] = {1, -1};
paddw(A, A, B);
EXPECT_EQ(SHRT_MIN, A[0]);
EXPECT_EQ(SHRT_MAX, A[1]);
}
TEST(paddsw, testOverflow_saturates) {
short A[8] = {SHRT_MAX, SHRT_MIN};
short B[8] = {1, -1};
paddsw(A, A, B);
EXPECT_EQ(SHRT_MAX, A[0]);
EXPECT_EQ(SHRT_MIN, A[1]);
TEST(x87, fpremFlags) {
uint32_t sw = 0xffff;
ASSERT_LDBL_EQ(1, fprem(1, -1.5, &sw));
ASSERT_EQ(0b1011100011111111, sw);
}

View file

@ -0,0 +1,28 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/errno.h"
#include "libc/testlib/testlib.h"
#include "tool/build/lib/xlaterrno.h"
TEST(xlaterrno, test) {
EXPECT_EQ(95, XlatErrno(EOPNOTSUPP));
EXPECT_EQ(90, XlatErrno(EMSGSIZE));
EXPECT_EQ(133, XlatErrno(EHWPOISON));
}

View file

@ -5,12 +5,14 @@ PKGS += TEST_TOOL_VIZ_LIB
TEST_TOOL_VIZ_LIB_SRCS := $(wildcard test/tool/viz/lib/*.c)
TEST_TOOL_VIZ_LIB_SRCS_TEST = $(filter %_test.c,$(TEST_TOOL_VIZ_LIB_SRCS))
TEST_TOOL_VIZ_LIB_COMS = $(TEST_TOOL_VIZ_LIB_OBJS:%.o=%.com)
TEST_TOOL_VIZ_LIB_OBJS = \
$(TEST_TOOL_VIZ_LIB_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_TOOL_VIZ_LIB_SRCS:%.c=o/$(MODE)/%.o)
TEST_TOOL_VIZ_LIB_COMS = \
$(TEST_TOOL_VIZ_LIB_SRCS:%.c=o/$(MODE)/%.com)
TEST_TOOL_VIZ_LIB_BINS = \
$(TEST_TOOL_VIZ_LIB_COMS) \
$(TEST_TOOL_VIZ_LIB_COMS:%=%.dbg)