Skip to content

Commit

Permalink
Fix isbn exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
jorg-vr committed Jul 30, 2024
1 parent 2ec43b9 commit 7eaad24
Show file tree
Hide file tree
Showing 8 changed files with 452 additions and 135 deletions.
23 changes: 19 additions & 4 deletions tested/languages/cpp/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pathlib import Path
from tested.features import Construct

from tested.features import Construct, TypeSupport
from tested.datatypes import AllTypes
from tested.languages.c.config import C
from tested.languages.conventionalize import Conventionable, NamingConventions
from tested.languages.cpp.generators import CPPGenerator
Expand All @@ -10,7 +10,7 @@

class CPP(C):
def initial_dependencies(self) -> list[str]:
return ["values.h", "values.cpp", "evaluation_result.h", "evaluation_result.cpp"]
return ["values.h", "values.cpp", "values.tpp", "evaluation_result.h", "evaluation_result.cpp"]

def file_extension(self) -> str:
return "cpp"
Expand All @@ -29,6 +29,21 @@ def supported_constructs(self) -> set[Construct]:
Construct.ASSIGNMENTS,
Construct.GLOBAL_VARIABLES,
Construct.OBJECTS,
Construct.HETEROGENEOUS_COLLECTIONS,
Construct.DEFAULT_PARAMETERS,
Construct.HETEROGENEOUS_ARGUMENTS
}

def datatype_support(self) -> dict[AllTypes, TypeSupport]:
return super().datatype_support() | { # type: ignore
"sequence": "supported",
"set": "supported",
"map": "supported",
"dictionary": "supported",
"object": "reduced",
"array": "supported",
"list": "supported",
"tuple": "supported",
}

def compilation(self, files: list[str]) -> CallbackResult:
Expand All @@ -38,7 +53,7 @@ def compilation(self, files: list[str]) -> CallbackResult:
return (
[
"g++",
"-std=c++11",
"-std=c++17",
"-Wall",
"-O3" if self.config.options.compiler_optimizations else "-O0",
"evaluation_result.cpp",
Expand Down
24 changes: 22 additions & 2 deletions tested/languages/cpp/generators.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

from tested.datatypes import AllTypes, resolve_to_basic
from tested.datatypes.advanced import AdvancedObjectTypes, AdvancedSequenceTypes, AdvancedStringTypes
from tested.datatypes.basic import BasicObjectTypes, BasicSequenceTypes, BasicTypes
from tested.datatypes.basic import BasicObjectTypes, BasicSequenceTypes, BasicStringTypes, BasicTypes
from tested.languages.c.generators import CGenerator
from tested.languages.preparation import PreparedExecutionUnit, PreparedFunctionCall, PreparedTestcase, PreparedTestcaseStatement
from tested.serialisation import FunctionCall, FunctionType, ObjectType, PropertyAssignment, SequenceType, Statement, VariableAssignment, VariableType, WrappedAllTypes
from tested.serialisation import FunctionCall, FunctionType, ObjectType, PropertyAssignment, SequenceType, Statement, Value, VariableAssignment, VariableType, WrappedAllTypes


class CPPGenerator(CGenerator):
Expand Down Expand Up @@ -41,6 +41,18 @@ def convert_map_subtypes(self, value: Statement, subtype: WrappedAllTypes) -> tu
value_type_str = self.convert_declaration(value_base_type, None, value_sub_type)

return key_type_str, value_type_str

def convert_value(self, value: Value) -> str:
tp = value.type
basic = resolve_to_basic(tp)
if basic == BasicObjectTypes.MAP:
return "{" + ", ".join(f"{self.convert_value(k), self.convert_value(v)}" for k, v in value.data.items()) + "}"
elif basic == BasicSequenceTypes.SEQUENCE or basic == BasicSequenceTypes.SET:
return "{" + ", ".join(self.convert_value(v) for v in value.data) + "}"
elif basic == BasicStringTypes.TEXT:
return f'std::string("{value.data}")'

return super().convert_value(value)



Expand Down Expand Up @@ -73,6 +85,14 @@ def convert_declaration(self, tp: AllTypes | VariableType,
elif basic == BasicSequenceTypes.SET:
subtype = self.convert_sequence_subtype(value, subtype)
return f"std::set<{subtype}>"
elif basic == BasicSequenceTypes.SEQUENCE:
subtype = self.convert_sequence_subtype(value, subtype)
return f"std::vector<{subtype}>"
elif basic == BasicStringTypes.TEXT:
return "std::string"
elif basic == BasicStringTypes.ANY:
return "std::any"


return super().convert_declaration(tp)

Expand Down
106 changes: 4 additions & 102 deletions tested/languages/cpp/templates/values.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdarg>
#include <cassert>
#include <cmath>
#include <stdarg.h>

#include "values.h"

using namespace std;

using namespace std;

// Function to escape special characters in a string
string escape(const string &buffer) {
Expand All @@ -31,7 +25,8 @@ string escape(const string &buffer) {
return dest;
}

#define FORMAT(name, x) "{\"type\": \"" name "\", \"data\":" x "}"



void write_formatted(FILE* out, const char* format, ...) {
va_list args;
Expand All @@ -40,99 +35,6 @@ void write_formatted(FILE* out, const char* format, ...) {
va_end(args);
}

void write_value(FILE* out, bool value) {
write_formatted(out, FORMAT("boolean", "%s"), value ? "true" : "false");
}

void write_value(FILE* out, char value) {
string buffer(1, value);
string result = escape(buffer);
write_formatted(out, FORMAT("char", "\"%s\""), result.c_str());
}

void write_value(FILE* out, signed char value) {
write_formatted(out, FORMAT("int8", "%d"), static_cast<int>(value));
}

void write_value(FILE* out, unsigned char value) {
write_formatted(out, FORMAT("uint8", "%u"), static_cast<unsigned int>(value));
}

void write_value(FILE* out, short int value) {
write_formatted(out, FORMAT("int16", "%d"), value);
}

void write_value(FILE* out, unsigned short int value) {
write_formatted(out, FORMAT("uint16", "%u"), value);
}

void write_value(FILE* out, int value) {
write_formatted(out, FORMAT("int32", "%d"), value);
}

void write_value(FILE* out, unsigned int value) {
write_formatted(out, FORMAT("uint32", "%u"), value);
}

void write_value(FILE* out, long value) {
write_formatted(out, FORMAT("int64", "%ld"), value);
}

void write_value(FILE* out, unsigned long value) {
write_formatted(out, FORMAT("uint64", "%lu"), value);
}

void write_value(FILE* out, long long value) {
write_formatted(out, FORMAT("integer", "%lld"), value);
}

void write_value(FILE* out, unsigned long long value) {
write_formatted(out, FORMAT("bigint", "%llu"), value);
}

void write_value(FILE* out, float value) {
if (isnan(value)) {
write_formatted(out, FORMAT("single_precision", "\"%s\""), "nan");
} else if (isinf(value)) {
write_formatted(out, FORMAT("single_precision", "\"%s\""), value < 0 ? "-inf" : "inf");
} else {
write_formatted(out, FORMAT("single_precision", "%f"), value);
}
}

void write_value(FILE* out, double value) {
if (isnan(value)) {
write_formatted(out, FORMAT("double_precision", "\"%s\""), "nan");
} else if (isinf(value)) {
write_formatted(out, FORMAT("double_precision", "\"%s\""), value < 0 ? "-inf" : "inf");
} else {
write_formatted(out, FORMAT("double_precision", "%lf"), value);
}
}

void write_value(FILE* out, long double value) {
if (isnan(value)) {
write_formatted(out, FORMAT("double_extended", "\"%s\""), "nan");
} else if (isinf(value)) {
write_formatted(out, FORMAT("double_extended", "\"%s\""), value < 0 ? "-inf" : "inf");
} else {
write_formatted(out, FORMAT("double_extended", "%Lf"), value);
}
}

void write_value(FILE* out, const string &value) {
string result = escape(value);
write_formatted(out, FORMAT("text", "\"%s\""), result.c_str());
}

void write_value(FILE* out, void* value) {
write_formatted(out, FORMAT("nothing", "\"%s\""), "null");
}

template <typename T> void write_value(FILE* out, T value)
{
write_formatted(out, FORMAT("unknown", "%p"), "?");
}

// Function to write evaluated results
void write_evaluated(FILE* out, EvaluationResult* result) {
Expand Down
46 changes: 22 additions & 24 deletions tested/languages/cpp/templates/values.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,33 @@
#include <vector>
#include <string>
#include <cstdio>
#include <set>
#include <map>
#include <list>
#include <tuple>
#include <iostream>
#include <string>
#include <typeinfo>
#include <type_traits>
#include <vector>
#include <set>
#include <map>
#include <array>
#include <list>
#include <tuple>
#include <any>

#include "evaluation_result.h"

// Function to escape special characters in a string
std::string escape(const std::string &buffer);
string escape(const string &buffer);

// Function to write a formatted string to an output stream
void write_formatted(FILE* out, const char* format, ...);

template <typename T> void write_value(FILE* out, T value);
void write_value(FILE* out, bool value);
void write_value(FILE* out, char value);
void write_value(FILE* out, signed char value);
void write_value(FILE* out, unsigned char value);
void write_value(FILE* out, short int value);
void write_value(FILE* out, unsigned short int value);
void write_value(FILE* out, int value);
void write_value(FILE* out, unsigned int value);
void write_value(FILE* out, long value);
void write_value(FILE* out, unsigned long value);
void write_value(FILE* out, long long value);
void write_value(FILE* out, unsigned long long value);
void write_value(FILE* out, float value);
void write_value(FILE* out, double value);
void write_value(FILE* out, long double value);
void write_value(FILE* out, const std::string &value);
void write_value(FILE* out, void* value);
void write_value(FILE* out, void* value);
// Function to write a value as a json object with type information to a file
template <typename T> void write_value(FILE* out, const T& value);

// Function to write evaluated results
void write_evaluated(FILE* out, EvaluationResult* result);

// Include the implementation file for template functions
#include "values.tpp"

#endif //WRITER_VALUES_H
Loading

0 comments on commit 7eaad24

Please sign in to comment.