Exemplo n.º 1
0
void SystemCore::deserializeActiveSystems(Tny* root, uint64_t referenceTime)
{
  if (root->type != TNY_DICT)
  {
    std::cerr << "es-system: Unexpected type during deserialization." << std::endl;
    throw std::runtime_error("Unexepected Tny type");
  }

  while (Tny_hasNext(root))
  {
    root = Tny_next(root);

    std::string name = root->key;
    if (root->type != TNY_OBJ)
    {
      std::cerr << "es-system: Unexpected type during deserialization." << std::endl;
      throw std::runtime_error("Unexpected Tny type");
    }

    Tny* comp = root->value.tny;

    if (comp->type != TNY_DICT)
    {
      std::cerr << "es-system: Unexpected type during deserialization." << std::endl;
      throw std::runtime_error("Unexpected Tny type");
    }
    Tny* val = Tny_get(comp, "interval");
    uint64_t interval = val->value.num;

    Tny_get(comp, "stagger");
    uint64_t stagger = val->value.num;

    // We ignore next exec and calculate it ourselves.
    //Tny_get(comp, "nextExec");
    //uint64_t nextExec = val->value.num;

    addActiveSystem(name, interval, referenceTime, stagger);
  }
}
Exemplo n.º 2
0
int main(void)
{
	Tny *root = NULL;
	Tny *embedded = NULL;
	Tny *tmp = NULL;
	char *message = "Message";
	uint32_t ui32 = 0xB16B00B5;
	uint64_t ui64 = 0xDEADBEEFABAD1DEAlu;
	char c = 'A';
	double flt = 13.37f;
	char *typesStr[] = {"BINARY", "INT32", "INT64", "CHAR", "NULL", "DOUBLE"};
	TnyType types[] = {TNY_BIN, TNY_INT32, TNY_INT64, TNY_CHAR, TNY_NULL, TNY_DOUBLE};
	void *values[] = {message, &ui32, &ui64, &c, NULL, &flt};
	size_t sizes[] = {strlen(message),0, 0, 0, 0, 0};
	char *keys[] = {"Key1", "Key2", "Key3", "Key4", "Key5", "Key6"};
	char corruptedObj[] = {0x01, 0x01, 0x00, 0x00, 0x00, 0x04, 0x08, 0x00, 0x00,
						   0x00, 0x4D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65};
	int counter = 0;
	int errors = 0;


	// Checking every datatype in an array.
	for (int i = 0; i < 6; i++) {
		root = Tny_add(NULL, TNY_ARRAY, NULL, NULL, 0);
		root = Tny_add(root, types[i], NULL, values[i], sizes[i]);
		if(serialize_deserialize(root)) {
			printf("Test with type %s in an array failed.\n", typesStr[i]);
			errors++;
		}
		TnyElement_free(root);
	}

	// Checking every datatype in a dictionary.
	for (int i = 0; i < 6; i++) {
		root = Tny_add(NULL, TNY_DICT, NULL, NULL, 0);
		root = Tny_add(root, types[i], keys[i], values[i], sizes[i]);
		if(serialize_deserialize(root)) {
			printf("Test with type %s in a dictionary failed.\n", typesStr[i]);
			errors++;
		}
		TnyElement_free(root);
	}

	// Adding every datatype to an array.
	root = Tny_add(NULL, TNY_ARRAY, NULL, NULL, 0);
	for (int i = 0; i < 6; i++) {
		root = Tny_add(root, types[i], NULL, values[i], sizes[i]);
	}
	if (serialize_deserialize(root)) {
		printf("Test with all types in an array failed.\n");
		errors++;
	}
	TnyElement_free(root);

	// Adding every datatype to a dictionary.
	root = Tny_add(NULL, TNY_DICT, NULL, NULL, 0);
	for (int i = 0; i < 6; i++) {
		root = Tny_add(root, types[i], keys[i], values[i], sizes[i]);
	}
	if (serialize_deserialize(root)) {
		printf("Test with all types in a dictionary failed.\n");
		errors++;
	}
	TnyElement_free(root);


	// Adding a dictionary to an array.
	root = Tny_add(NULL, TNY_ARRAY, NULL, NULL, 0);
	for (int i = 0; i < 6; i++) {
		root = Tny_add(root, types[i], NULL, values[i], sizes[i]);
	}
	embedded = Tny_add(NULL, TNY_DICT, NULL, NULL, 0);
	for (int i = 0; i < 6; i++) {
		embedded = Tny_add(embedded, types[i], keys[i], values[i], sizes[i]);
	}
	Tny_add(root, TNY_OBJ, NULL, embedded, 0);
	if (serialize_deserialize(root)) {
		printf("Test with a sub document in an array failed.\n");
		errors++;
	}

	if (root != NULL && embedded != NULL) {
		// Fetching a random element from an array
		tmp = Tny_at(root, 2);
		if (tmp == NULL || tmp->value.num != *(uint64_t*)values[2]) {
			printf("Fetching element at position 2 failed!\n");
			errors++;
		}

		// Fetching a random element from a dictionary
		tmp = Tny_get(embedded, "Key3");
		if (tmp == NULL || tmp->value.num != *(uint64_t*)values[2]) {
			printf("Fetching element with key 'Key3' failed!\n");
			errors++;
		}

		// Remove an element.
		Tny_remove(Tny_at(root, 2));
		tmp = Tny_at(root, 2);
		if (tmp == NULL || tmp->value.chr != *(char*)values[3]) {
			printf("Deleting element at position 2 failed!\n");
			errors++;
		}

		// Adding element after position 2
		ui32 = 0x12345678;
		tmp = Tny_at(root, 2);
		Tny_add(tmp, TNY_INT32, NULL, &ui32, 0);
		tmp = Tny_at(root, 3);
		if (tmp == NULL || tmp->value.num != ui32) {
			printf("Adding element after position 2 failed!\n");
			errors++;
		}
	}
	TnyElement_free(root);


	// Testing the iterator functions.
	root = Tny_add(NULL, TNY_ARRAY, NULL, NULL, 0);
	for (uint32_t i = 0; i < 10; i++) {
		root = Tny_add(root, TNY_INT32, NULL, &i, 0);
	}

	counter = 0;
	root = root->root;
	while (Tny_hasNext(root)) {
		root = Tny_next(root);
		if (root->value.num != counter) {
			printf("Iterator Test (1) failed!\n");
			errors++;
			break;
		}
		counter++;
	}
	if (counter != root->root->size) {
		printf("Iterator Test (2) failed!\n");
		errors++;
	}
	TnyElement_free(root);


	// Loading a corrupted document containing a corrupted size field.
	root = Tny_loads(corruptedObj, sizeof(corruptedObj));
	if (root == NULL || root->size != 0) {
		printf("Loading of a corrupted document failed!\n");
		errors++;
	}
	TnyElement_free(root);

	printf("Tny tests completed with %u error(s).\n", errors);

	return EXIT_SUCCESS;
}