diff options
author | RabsRincon <rubrinbla@gmail.com> | 2017-12-12 23:51:34 +0100 |
---|---|---|
committer | RabsRincon <rubrinbla@gmail.com> | 2017-12-12 23:51:34 +0100 |
commit | e6bd7148404d2178537da770b69a3800e727c6cc (patch) | |
tree | d190c7561d42fc548d21c1b1c3b7997340ff3223 | |
parent | e08faa589b023bde8bff2df9e4e15bdc4896e21b (diff) | |
download | compiler-explorer-e6bd7148404d2178537da770b69a3800e727c6cc.tar.gz compiler-explorer-e6bd7148404d2178537da770b69a3800e727c6cc.zip |
Add C & CPPX examples. Add load() builtin tests
-rw-r--r-- | examples/c/Max_array.c | 5 | ||||
-rw-r--r-- | examples/c/Sum_over_array.c | 7 | ||||
-rw-r--r-- | examples/cppx/Interface.cpp | 61 | ||||
-rw-r--r-- | test/builtin-tests.js | 28 |
4 files changed, 99 insertions, 2 deletions
diff --git a/examples/c/Max_array.c b/examples/c/Max_array.c new file mode 100644 index 000000000..bc2ef8799 --- /dev/null +++ b/examples/c/Max_array.c @@ -0,0 +1,5 @@ +void maxArray(double* x, double* y) { + for (int i = 0; i < 65536; i++) { + if (y[i] > x[i]) x[i] = y[i]; + } +} diff --git a/examples/c/Sum_over_array.c b/examples/c/Sum_over_array.c new file mode 100644 index 000000000..601ef1600 --- /dev/null +++ b/examples/c/Sum_over_array.c @@ -0,0 +1,7 @@ +int testFunction(int* input, int length) { + int sum = 0; + for (int i = 0; i < length; ++i) { + sum += input[i]; + } + return sum; +} diff --git a/examples/cppx/Interface.cpp b/examples/cppx/Interface.cpp new file mode 100644 index 000000000..1af545545 --- /dev/null +++ b/examples/cppx/Interface.cpp @@ -0,0 +1,61 @@ + +//==================================================================== +// Library code: implementing the metaclass (once) + +$class interface { + constexpr { + compiler.require($interface.variables().empty(), + "interfaces may not contain data"); + for... (auto f : $interface.functions()) { + compiler.require(!f.is_copy() && !f.is_move(), + "interfaces may not copy or move; consider a" + " virtual clone() instead"); + if (!f.has_access()) f.make_public(); + compiler.require(f.is_public(), + "interface functions must be public"); + f.make_pure_virtual(); + } + } + virtual ~interface() noexcept { } +}; + + +//==================================================================== +// User code: using the metaclass to write a type (many times) + +interface Shape { + int area() const; + void scale_by(double factor); +}; + + // try putting any of these lines into Shape to see "interface" rules + // enforced => using the metaclass name to declare intent makes + // this code more robust to such changes under maintenance + // + // int i; // error: interfaces may not contain data + // private: void g(); // error: interface functions must be public + // Shape(const Shape&); // error: interfaces may not copy or move; + // // consider a virtual clone() instead + +// Godbolt.org note: Click the "triangle ! icon" to see the output +constexpr { + compiler.debug($Shape); +} + + +//==================================================================== +// And then continue to use it as "just a class" as always... this is +// normal code just as if we'd written Shape not using a metaclass + +class Circle : public Shape { +public: + int area() const override { return 1; } + void scale_by(double factor) override { } +}; + +#include <memory> + +int main() { + std::unique_ptr<Shape> shape = std::make_unique<Circle>(); + shape->area(); +} diff --git a/test/builtin-tests.js b/test/builtin-tests.js index 98f11d2f9..4201abf8b 100644 --- a/test/builtin-tests.js +++ b/test/builtin-tests.js @@ -24,7 +24,8 @@ const should = require('chai').should(), builtin = require('../lib/sources/builtin'), - languages = require('../lib/languages').list; + languages = require('../lib/languages').list, + _ = require('underscore-node'); describe('Builtin sources', () => { it('Does not include default code', () => { @@ -37,7 +38,30 @@ describe('Builtin sources', () => { it('Has a valid language listed', () => { builtin.list((_, list) => { list.forEach(example => { - should.not.equal(languages[example.lang], undefined); + should.not.equal(languages[example.lang], undefined, `Builtin ${example.name} has unrecognised language ${example.lang}`); + }); + }); + }); + it('Has at least one example for each language', () => { + builtin.list((placeholder, list) => { + _.each(languages, lang => { + should.not.equal(_.find(list, elem => elem.lang === lang.id), undefined, `Language ${lang.name} does not have any builtins`); + }); + }); + }); + it('Reports a string error if no example found', () => { + builtin.load('BADLANG', 'BADFILE', (error, result) => { + should.equal(result, undefined, 'A result should not be returned for bad requests'); + error.should.be.a("string"); + }); + }); + it('Reports something for every defined example', () => { + builtin.list((placeholder, examples) => { + examples.forEach(example => { + builtin.load(example.lang, example.file, (error, result) => { + should.not.exist(error, `Can't read ${example.name} for ${example.lang} in ${example.file}`); + result.file.should.be.a('string'); + }); }); }); }); |