Skip to content

Commit d3d81c5

Browse files
committed
Working on tests
1 parent 6f1c4ea commit d3d81c5

19 files changed

+182
-100
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ third_party/spdlog/
2121
third_party/Static-Sort/
2222
third_party/THST/
2323

24-
program/
24+
program
2525

2626
fftw_wisdom_float.bin
2727
fftw_wisdom_double.bin

Tupfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ include_rules
33
run node util/definesWriter.js "@(NAME)"
44

55
# Compile sources
6-
: foreach $(SRC_PATH)/*.cpp | $(ROOT)/<gen_headers> |> !cpp |>
6+
: foreach $(SRC_PATH)/main.cpp | $(ROOT)/<gen_headers> |> !cpp |>
7+
: foreach $(SRC_PATH)/version.cpp | $(ROOT)/<gen_headers> |> !cpp "-DTSVIZ_VERSION=$(VERSION)" |>
78
: foreach $(SRC_PATH)/app/*.cpp | $(ROOT)/<gen_headers> |> !cpp |>
89
: foreach $(SRC_PATH)/builder/*.cpp | $(ROOT)/<gen_headers> |> !cpp |>
910
: foreach $(SRC_PATH)/graphics/*.cpp | $(ROOT)/<gen_headers> |> !cpp |>

Tuprules.tup

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ THIRD_PARTY_PATH = $(ROOT)/third_party
55

66
BIN_TARGET = $(ROOT)/ts-viz
77

8+
# VERSION = `cd .. && git add --all && git write-tree`
9+
VERSION = 0.1
10+
811
CFLAGS += -std=c++17
912
CFLAGS += -Wall -Werror -Wno-missing-braces -Wno-unused -Wno-expansion-to-defined
1013
CFLAGS += -I$(SRC_PATH) -I$(THIRD_PARTY_PATH) -I$(THIRD_PARTY_PATH)/spdlog/include -I$(THIRD_PARTY_PATH)/rapidjson/include

defines.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = (variant) => ({
1414

1515
PRINT_TICK_ORDER: '!defined(NDEBUG) && 0',
1616

17-
ENABLE_CHUNK_NAMES: '!defined(NDEBUG) && 1',
17+
ENABLE_CHUNK_NAMES: '!defined(NDEBUG) && 0',
1818

1919
ENABLE_CHUNK_MULTITHREADING: 0,
2020

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"dependencies": {
3-
"chroma-js": "^2.1.0",
43
"glob": "^7.1.6"
54
}
65
}

src/main.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "app/mainloop.h"
1111
#include "app/quitexception.h"
1212

13+
#include "version.h"
1314
#include "stream/filepoller.h"
1415
#include "stream/jsonunwrapper.h"
1516
#include "program/programmanager.h"
@@ -20,7 +21,7 @@
2021

2122
int main(int argc, char **argv) {
2223
// Setup argument parser
23-
argparse::ArgumentParser program("ts-viz");
24+
argparse::ArgumentParser program("ts-viz", tsVizVersion);
2425
program.add_description("A time series visualizer and processor");
2526

2627
program.add_argument("program-path")
@@ -59,9 +60,14 @@ int main(int argc, char **argv) {
5960
// Print some debug info
6061
#ifndef NDEBUG
6162
spdlog::warn("This is a debug build!");
63+
#else
64+
spdlog::info("This is a release build!");
65+
#endif
66+
67+
spdlog::info("Version is {}", tsVizVersion);
68+
6269
spdlog::info("A pointer is {} bits", sizeof(void*) * CHAR_BIT);
6370
spdlog::info("An unsigned int is {} bits", sizeof(unsigned int) * CHAR_BIT);
64-
#endif
6571

6672
spdlog::info("Running tests...");
6773
util::TestRunner::getInstance().run();

src/series/type/inputseries.h

+35-16
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,23 @@ class InputSeries : public DataSeries<ElementType> {
3333
});
3434
}
3535

36-
void set(std::size_t index, ElementType value) {
37-
std::size_t ctr = nextIndex;
38-
std::size_t prevChunk = ctr / CHUNK_SIZE;
39-
40-
assert(index >= ctr);
41-
while (ctr < index) {
42-
this->getChunk(ctr / CHUNK_SIZE)->getMutableData()[ctr % CHUNK_SIZE] = prevValue;
43-
ctr++;
44-
}
36+
void propagateUntil(std::size_t index) {
37+
wrapNotify([this, index]() {
38+
propagateUntilImpl(index);
39+
assert(nextIndex == index);
40+
});
41+
}
4542

46-
prevValue = value;
47-
this->getChunk(ctr / CHUNK_SIZE)->getMutableData()[ctr % CHUNK_SIZE] = value;
43+
void set(std::size_t index, ElementType value) {
44+
wrapNotify([this, index, value]() {
45+
propagateUntilImpl(index);
46+
assert(nextIndex == index);
4847

49-
nextIndex = ctr + 1;
48+
prevValue = value;
49+
this->getChunk(index / CHUNK_SIZE)->getMutableData()[index % CHUNK_SIZE] = value;
5050

51-
while (prevChunk <= ctr / CHUNK_SIZE) {
52-
this->getChunk(prevChunk)->notify();
53-
prevChunk++;
54-
}
51+
nextIndex++;
52+
});
5553
}
5654

5755
private:
@@ -62,6 +60,27 @@ class InputSeries : public DataSeries<ElementType> {
6260
#else
6361
std::size_t nextIndex = 0;
6462
#endif
63+
64+
void propagateUntilImpl(std::size_t index) {
65+
assert(nextIndex <= index);
66+
while (nextIndex < index) {
67+
this->getChunk(nextIndex / CHUNK_SIZE)->getMutableData()[nextIndex % CHUNK_SIZE] = prevValue;
68+
nextIndex++;
69+
}
70+
assert(nextIndex == index);
71+
}
72+
73+
template <typename FuncType>
74+
void wrapNotify(FuncType func) {
75+
std::size_t prevChunk = nextIndex / CHUNK_SIZE;
76+
77+
func();
78+
79+
while (prevChunk <= (nextIndex - 1) / CHUNK_SIZE) {
80+
this->getChunk(prevChunk)->notify();
81+
prevChunk++;
82+
}
83+
}
6584
};
6685

6786
}

src/stream/filepoller.h

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "readerwriterqueue/readerwriterqueue.h"
77

88
#include "app/appcontext.h"
9-
#include "app/mainloop.h"
109
#include "app/tickercontext.h"
1110

1211
namespace stream {

src/stream/inputmanager.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ void InputManager::recvRecord(const rapidjson::Document &row) {
5757
void InputManager::end() {
5858
assert(running);
5959
running = false;
60+
61+
for (const std::pair<std::string, series::InputSeries<INPUT_SERIES_ELEMENT_TYPE> *> entry : inputs) {
62+
entry.second->propagateUntil(index);
63+
}
6064
}
6165

6266

src/stream/outputmanager.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "rapidjson/stringbuffer.h"
77
#include "rapidjson/writer.h"
88

9-
#include "app/mainloop.h"
9+
#include "app/options.h"
1010
#include "stream/inputmanager.h"
1111

1212
namespace stream {
@@ -33,6 +33,10 @@ void OutputManager::tick(app::TickerContext &tickerContext) {
3333
}
3434

3535
void OutputManager::emit() {
36+
if (emitters.empty()) {
37+
return;
38+
}
39+
3640
static thread_local rapidjson::StringBuffer buffer;
3741
static thread_local rapidjson::Writer<rapidjson::StringBuffer> writer;
3842

@@ -64,7 +68,7 @@ void OutputManager::emit() {
6468

6569
bool OutputManager::isRunning() const {
6670
assert(nextEmitIndex <= context.get<InputManager>().getIndex());
67-
return nextEmitIndex < context.get<InputManager>().getIndex();
71+
return nextEmitIndex < context.get<InputManager>().getIndex() && !emitters.empty();
6872
}
6973

7074
}

src/version.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "version.h"
2+
3+
#define STRINGIFY2(x) #x
4+
#define STRINGIFY(x) STRINGIFY2(x)
5+
6+
const char *tsVizVersion = STRINGIFY(TSVIZ_VERSION);

src/version.h

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
extern const char *tsVizVersion;

tests/simple-spikes.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,16 @@ const {
4040
const r = d;
4141

4242
module.exports = [
43-
{
43+
...[
44+
// ...[2, 3, 4, 7, 8, 9],
45+
// ...[10, 15, 16, 17, 31, 32, 33],
46+
// ...[63, 64, 65, 127, 128, 129, 255, 256, 257],
47+
63,
48+
].map((n) => ({
49+
name: `Test simple conv for n=${n}`,
4450
variant: 'test-csl2-6',
45-
input: { 0: { x: 0 }, 10: { x: 1 }, 11: { x: 0 }, 63: {} },
46-
program: conv(windowRect(r(16)), r(input('x')), true),
47-
output: { 0: { x: 0 }, 63: {} },
48-
},
51+
input: { 0: { x: 0 }, 10: { x: 1 }, 11: { x: 0 }, 300: {} },
52+
program: conv(windowRect(r(n)), r(input('x')), true),
53+
output: { 0: { z: 0 }, 10: { z: 1 / n }, [10 + n]: { z: 0 }, 300: {} },
54+
})),
4955
];

ts-viz.cxxflags

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
-std=c++17
1+
-std=c++17 -DTSVIZ_VERSION=unset

ts-viz.files

+2
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,5 @@ src/util/testrunner.cpp
175175
src/util/testrunner.h
176176
src/util/transformers.h
177177
src/util/uniquetuple.h
178+
src/version.cpp
179+
src/version.h

util/jsonsDiff.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const fs = require('fs');
2+
const readline = require('readline');
3+
4+
if (process.argv.length !== 4) {
5+
throw new Error(`Usage: node ${process.argv[1]} [file A] [file B]`);
6+
}
7+
8+
const EPSILON = 1e-9;
9+
10+
const isFuzzyEqual = (a, b) => {
11+
if (typeof a === 'number' && typeof b === 'number') {
12+
return (isNaN(a) && isNaN(b)) || Math.abs(a - b) < EPSILON;
13+
} else if (typeof a === 'object' && typeof b === 'object') {
14+
for (key in a) {
15+
if (!isFuzzyEqual(a[key], b[key])) {
16+
return false;
17+
}
18+
}
19+
for (key in b) {
20+
if (!isFuzzyEqual(a[key], b[key])) {
21+
return false;
22+
}
23+
}
24+
return true;
25+
} else {
26+
return a === b;
27+
}
28+
};
29+
30+
const cmpLines = (index, a, b) => {
31+
if (!isFuzzyEqual(a, b)) {
32+
a && console.log('\x1b[31m%s\x1b[0m', `${index}: - ${JSON.stringify(a)}`);
33+
b && console.log('\x1b[32m%s\x1b[0m', `${index}: + ${JSON.stringify(b)}`);
34+
process.exitCode = 1;
35+
}
36+
};
37+
38+
const lines = [[], []];
39+
const filePromises = [process.argv[2], process.argv[3]].map(
40+
(filename, fileIndex) => {
41+
const rl = readline.createInterface({
42+
input: fs.createReadStream(filename),
43+
terminal: false,
44+
});
45+
46+
let lineIndex = 0;
47+
rl.on('line', (line) => {
48+
const data = JSON.parse(line);
49+
lines[fileIndex][lineIndex] = data;
50+
if (lines[1 - fileIndex].length > lineIndex) {
51+
cmpLines(lineIndex, lines[0][lineIndex], lines[1][lineIndex]);
52+
}
53+
54+
lineIndex++;
55+
});
56+
57+
return new Promise((resolve) => rl.on('close', resolve));
58+
},
59+
);
60+
61+
Promise.all(filePromises).then(() => {
62+
const min = Math.min(lines[0].length, lines[1].length);
63+
const max = Math.max(lines[0].length, lines[1].length);
64+
for (let i = min; i < max; i++) {
65+
cmpLines(i, lines[0][i], lines[1][i]);
66+
}
67+
});

util/run_test.bash

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
#!/bin/bash
22

3-
set -e
4-
set -x
3+
# set -e
4+
# set -x
55

6-
# echo "Binary: $1"
7-
# echo "Input: $2"
8-
# echo "Program: $3"
9-
# echo "Output: $4"
6+
# echo "Test Name: $1"
7+
# echo "Binary: $2"
8+
# echo "Input: $3"
9+
# echo "Program: $4"
10+
# echo "Output: $5"
1011

1112
TMP_DIR=$(mktemp -d)
12-
printf "$2" > $TMP_DIR/input.jsons
13-
printf "$3" > $TMP_DIR/program.jsons
14-
printf "$4" | cat -n > $TMP_DIR/expected_output.jsons
13+
printf "$3" > $TMP_DIR/input.jsons
14+
printf "$4" > $TMP_DIR/program.jsons
15+
printf "$5" > $TMP_DIR/expected_output.jsons
1516

16-
"$1" --dont-write-wisdom $TMP_DIR/program.jsons $TMP_DIR/input.jsons | cat -n > $TMP_DIR/actual_output.jsons
17+
"$2" --dont-write-wisdom $TMP_DIR/program.jsons $TMP_DIR/input.jsons > $TMP_DIR/actual_output.jsons
1718

18-
git diff --color $TMP_DIR/expected_output.jsons $TMP_DIR/actual_output.jsons
19+
if [ $? -ne 0 ]; then
20+
echo -e "\033[0;31mCOMMAND FAILED\033[0m: $1"
21+
echo "Keeping temporary files, command was:"
22+
echo "$2" --dont-write-wisdom $TMP_DIR/program.jsons $TMP_DIR/input.jsons
23+
exit 1
24+
fi
1925

20-
# rm -rf $TMP_DIR
26+
node util/jsonsDiff.js $TMP_DIR/actual_output.jsons $TMP_DIR/expected_output.jsons
27+
28+
if [ $? -ne 0 ]; then
29+
echo -e "\033[0;31mDIFF FAILED\033[0m: $1"
30+
echo "Keeping temporary files, command was:"
31+
echo "$2" --dont-write-wisdom $TMP_DIR/program.jsons $TMP_DIR/input.jsons
32+
exit 1
33+
fi
34+
35+
echo -e "\033[0;32mPASSED\033[0m: $1"
36+
rm -rf $TMP_DIR

util/testsWriter.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ const variant = process.argv[2];
88
const processJsonStream = (spec) => {
99
const arr = [];
1010
Object.entries(spec).forEach(([k, v]) => (arr[k] = v));
11+
let acc = {};
1112
for (let i = 0; i < arr.length; i++) {
12-
if (arr[i] === undefined) {
13-
arr[i] = {};
14-
}
13+
arr[i] = acc = { ...acc, ...arr[i] };
1514
}
1615
return serializeLines(arr.map((x) => JSON.stringify(x)));
1716
};
@@ -34,13 +33,13 @@ const serializeLines = (lines) => lines.map((line) => line + '\\n').join('');
3433

3534
tests
3635
.filter((test) => test.variant === variant)
37-
.forEach(({ input, program, output }) => {
36+
.forEach(({ name, input, program, output }) => {
3837
input = processJsonStream(input);
3938
program = processProgram(program);
4039
output = processJsonStream(output);
4140

4241
console.log(
43-
`: $(BIN_TARGET) |> bash util/run_test.bash '$(BIN_TARGET)' '${input}' '${program}' '${output}' |>`,
42+
`: $(BIN_TARGET) |> bash util/run_test.bash '${name}' '$(BIN_TARGET)' '${input}' '${program}' '${output}' |>`,
4443
);
4544
});
4645
});

0 commit comments

Comments
 (0)