Ejemplo n.º 1
0
/*
 * Initialize the buffers
 */
static void init_test2(void) {
  rational_t q0;
  uint32_t i;

  q_init(&q0);
  for (i=0; i<8; i++) {
    init_rba_buffer(aux + i, &prod_table);
  }

  rba_buffer_add_var(&aux[0], 3); // x_3

  q_set32(&q0, 2);
  rba_buffer_add_const(&aux[1], &q0); // 2

  rba_buffer_add_var(&aux[2], 1);
  rba_buffer_sub_var(&aux[2], 2); // x_1 - x_2

  rba_buffer_add_var(&aux[3], 0);
  rba_buffer_sub_const(&aux[3], &q0); // x_0 - 2

  rba_buffer_add_pp(&aux[4], pprod_mul(&prod_table, var_pp(1), var_pp(1))); // x_1^2

  rba_buffer_add_var(&aux[5], 0);
  rba_buffer_mul_const(&aux[5], &q0); // 2 * x_0

  rba_buffer_add_varmono(&aux[6], &q0, 1); // 2 * x_1

  rba_buffer_sub_var(&aux[7], 3);
  rba_buffer_sub_var(&aux[7], 3);
  rba_buffer_add_var(&aux[7], 4);

  q_clear(&q0);
}
Ejemplo n.º 2
0
int main(void) {
  init_rationals();
  init_pprod_table(&ptable, 0);
  init_rba_buffer(&buffer, &ptable);

  run_tests(&buffer);

  delete_rba_buffer(&buffer);
  delete_pprod_table(&ptable);
  cleanup_rationals();
  return 0;
}
Ejemplo n.º 3
0
/*
 * Test binary operations with b1 and b2
 */
static void test_ops(rba_buffer_t *b1, rba_buffer_t *b2) {
  rba_buffer_t b;

  printf("b1: ");
  print_rba_buffer(stdout, b1);
  printf("\nb2: ");
  print_rba_buffer(stdout, b2);
  printf("\n");

  printf("Equality test: ");
  if (rba_buffer_equal(b1, b2)) {
    printf("yes\n");
  } else {
    printf("no\n");
  }

  init_rba_buffer(&b, &prod_table);

  reset_rba_buffer(&b);
  rba_buffer_add_buffer(&b, b1);
  rba_buffer_add_buffer(&b, b2);
  printf("  b1 + b2: ");
  print_rba_buffer(stdout, &b);
  printf("\n");

  reset_rba_buffer(&b);
  rba_buffer_add_buffer(&b, b1);
  rba_buffer_sub_buffer(&b, b2);
  printf("  b1 - b2: ");
  print_rba_buffer(stdout, &b);
  printf("\n");

  reset_rba_buffer(&b);
  rba_buffer_add_buffer(&b, b2);
  rba_buffer_sub_buffer(&b, b1);
  printf("  b2 - b1: ");
  print_rba_buffer(stdout, &b);
  printf("\n");

  reset_rba_buffer(&b);
  rba_buffer_add_buffer(&b, b1);
  rba_buffer_mul_buffer(&b, b2);
  printf("  b1 * b2: ");
  print_rba_buffer(stdout, &b);
  printf("\n");

  reset_rba_buffer(&b);
  rba_buffer_add_buffer(&b, b2);
  rba_buffer_mul_buffer(&b, b1);
  printf("  b2 * b1: ");
  print_rba_buffer(stdout, &b);
  printf("\n");

  reset_rba_buffer(&b);
  rba_buffer_add_buffer_times_buffer(&b, b1, b2);
  printf("  b1 * b2: ");
  print_rba_buffer(stdout, &b);
  printf("\n");

  reset_rba_buffer(&b);
  rba_buffer_sub_buffer_times_buffer(&b, b1, b2);
  printf("- b1 * b2: ");
  print_rba_buffer(stdout, &b);
  printf("\n");

  delete_rba_buffer(&b);

  printf("----\n");
}
Ejemplo n.º 4
0
/*
 * Tests: one buffer
 */
static void test1(void) {
  rba_buffer_t buffer;
  rational_t q0;

  q_init(&q0);
  init_rba_buffer(&buffer, &prod_table);
  printf("Empty buffer\n");
  test_buffer(&buffer);

  printf("x_0 + x_1\n");
  rba_buffer_add_var(&buffer, 0);
  rba_buffer_add_var(&buffer, 1);
  test_buffer(&buffer);

  printf("After reset\n");
  reset_rba_buffer(&buffer);
  test_buffer(&buffer);

  printf("x_2 - x_0\n");
  rba_buffer_add_var(&buffer, 2);
  rba_buffer_sub_var(&buffer, 0);
  test_buffer(&buffer);

  printf("x_2 - x_0 + x_1 + x_0\n");
  reset_rba_buffer(&buffer);
  rba_buffer_add_var(&buffer, 2);
  rba_buffer_sub_var(&buffer, 0);
  rba_buffer_add_var(&buffer, 1);
  rba_buffer_add_var(&buffer, 0);
  test_buffer(&buffer);

  printf("Adding 3\n");
  q_set32(&q0, 3);
  rba_buffer_add_const(&buffer, &q0);
  test_buffer(&buffer);

  printf("Negating\n");
  rba_buffer_negate(&buffer);
  test_buffer(&buffer);

  printf("Negating again\n");
  rba_buffer_negate(&buffer);
  test_buffer(&buffer);

  printf("Multiplying by 2 x_4\n");
  q_set32(&q0, 2);
  rba_buffer_mul_varmono(&buffer, &q0, 4);
  test_buffer(&buffer);

  printf("Multiplying by x_1^2\n");
  rba_buffer_mul_var(&buffer, 1);
  rba_buffer_mul_var(&buffer, 1);
  test_buffer(&buffer);

  printf("Multiplying by 0\n");
  q_clear(&q0);
  rba_buffer_mul_const(&buffer, &q0);
  test_buffer(&buffer);

  printf("x_1 + 1 - x_2\n");
  reset_rba_buffer(&buffer);
  rba_buffer_add_var(&buffer, 1);
  q_set32(&q0, 1);
  rba_buffer_add_const(&buffer, &q0);
  rba_buffer_sub_var(&buffer, 2);
  test_buffer(&buffer);

  printf("Squaring\n");
  rba_buffer_square(&buffer);
  test_buffer(&buffer);

  printf("Squaring\n");
  rba_buffer_square(&buffer);
  test_buffer(&buffer);

  printf("Squaring\n");
  rba_buffer_square(&buffer);
  test_buffer(&buffer);

  q_clear(&q0);
  delete_rba_buffer(&buffer);
}