Beispiel #1
0
static void
testFill()
{
  Storage_T s3 = Storage_new(3);
  Storage_fill(s3, 27);
  EXPECT_EQ_DOUBLE(27.0, Storage_get(s3, 0), 0);
  EXPECT_EQ_DOUBLE(27.0, Storage_get(s3, 1), 0);
  EXPECT_EQ_DOUBLE(27.0, Storage_get(s3, 2), 0);
}
Beispiel #2
0
static void
testGetSet()
{
  Storage_T s3 = Storage_new(3);
  Storage_set(s3, 0, 10);
  Storage_set(s3, 1, 11);
  Storage_set(s3, 2, 12.0);
  EXPECT_EQ_DOUBLE(10.0, Storage_get(s3, 0), 0);
  EXPECT_EQ_DOUBLE(11.0, Storage_get(s3, 1), 0);
  EXPECT_EQ_DOUBLE(12.0, Storage_get(s3, 2), 0);
}
Beispiel #3
0
static void
testApply()
{
  Storage_T s3 = Storage_new(3);
  Storage_set(s3, 0, 10);
  Storage_set(s3, 1, 20);
  Storage_set(s3, 2, 30);
  double constant = 1;
  Storage_apply(s3, doubleAddConstant, &constant);
  EXPECT_EQ_DOUBLE(21, Storage_get(s3, 0), 0);
  EXPECT_EQ_DOUBLE(41, Storage_get(s3, 1), 0);
  EXPECT_EQ_DOUBLE(61, Storage_get(s3, 2), 0);
}
Beispiel #4
0
static void testKnnSmooth() {
  const int trace = 0;
  unsigned nObs;
  unsigned nDims;
  double *inputsP;
  double *targetsP;
  makeData(&nObs, &nDims, &inputsP, &targetsP);

  // smooth each target for k == 1
  {
    const unsigned k = 1;
    double expected[] = {0.0, 1.0, 2.0, 3.0};
    for (unsigned queryIndex = 0; queryIndex < nObs; queryIndex++) {
      double estimate = 
        Knn_smooth(nObs, nDims, 
                   inputsP, targetsP, 
                   k, queryIndex); 
      if (trace) printf("queryIndex=%u\n", queryIndex);
      EXPECT_EQ_DOUBLE(expected[queryIndex], estimate, 1e-10);
    }
  }

  // smooth each target for k == 2
  {
    const unsigned k = 2;
    double expected[] = {0.5, 0.5, 1.5, 2.5};
    for (unsigned queryIndex = 0; queryIndex < nObs; queryIndex++) {
      double estimate = 
        Knn_smooth(nObs, nDims, 
                   inputsP, targetsP, 
                   k, queryIndex); 
      if (trace) printf("queryIndex=%u\n", queryIndex);
      EXPECT_EQ_DOUBLE(expected[queryIndex], estimate, 1e-10);
    }
  }

  // smooth each target for k == 3
  {
    const unsigned k = 3;
    double expected[] = {1.0, 1.0, 2.0, 2.0};
    for (unsigned queryIndex = 0; queryIndex < nObs; queryIndex++) {
      double estimate = 
        Knn_smooth(nObs, nDims, 
                   inputsP, targetsP, 
                   k, queryIndex); 
      if (trace) printf("queryIndex=%u\n", queryIndex);
      EXPECT_EQ_DOUBLE(expected[queryIndex], estimate, 1e-10);
    }
  }
}
Beispiel #5
0
static void
testNewCopy()
{
  Storage_T s3 = Storage_new(3);
  Storage_set(s3, 0, 10.0);
  Storage_set(s3, 1, 11.0);
  Storage_set(s3, 2, 12.0);
  Storage_T c = Storage_newCopy(s3);  // the copy shares nothing
  Storage_free(&s3);
  EXPECT_EQ_UNSIGNED(3, Storage_size(c));
  EXPECT_EQ_UNSIGNED(1, Storage_nReferences(c));
  EXPECT_EQ_DOUBLE(10.0, Storage_get(c, 0), 0);
  EXPECT_EQ_DOUBLE(11.0, Storage_get(c, 1), 0);
  EXPECT_EQ_DOUBLE(12.0, Storage_get(c, 2), 0);
  Storage_free(&c);
}
Beispiel #6
0
static void test_access_number() {
    lept_value v;
    lept_init(&v);
    lept_set_string(&v, "a", 1);
    lept_set_number(&v, 1234.5);
    EXPECT_EQ_DOUBLE(1234.5, lept_get_number(&v));
    lept_free(&v);
}
Beispiel #7
0
static void
testResize()
{
  Storage_T s3 = Storage_new(3);
  Storage_set(s3, 0, 10.0);
  Storage_set(s3, 1, 11.0);
  Storage_set(s3, 2, 12.0);

  Storage_T r1 = Storage_newCopy(s3);  // the copy shares nothing
  Storage_resize(r1, 1);
  EXPECT_EQ_UNSIGNED(1, Storage_size(r1));
  EXPECT_EQ_DOUBLE(10.0, Storage_get(r1, 0), 0);

  Storage_T r5 = Storage_newCopy(s3);  // the copy shares nothing
  Storage_resize(r5, 5);
  EXPECT_EQ_UNSIGNED(5, Storage_size(r5));
  EXPECT_EQ_DOUBLE(10.0, Storage_get(r5, 0), 0);
  EXPECT_EQ_DOUBLE(11.0, Storage_get(r5, 1), 0);
  EXPECT_EQ_DOUBLE(12.0, Storage_get(r5, 2), 0);
}
Beispiel #8
0
static void test_parse_array() {
    size_t i, j;
    lept_value v;

    lept_init(&v);
    EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "[ ]"));
    EXPECT_EQ_INT(LEPT_ARRAY, lept_get_type(&v));
    EXPECT_EQ_SIZE_T(0, lept_get_array_size(&v));
    lept_free(&v);

    lept_init(&v);
    EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "[ null , false , true , 123 , \"abc\" ]"));
    EXPECT_EQ_INT(LEPT_ARRAY, lept_get_type(&v));
    EXPECT_EQ_SIZE_T(5, lept_get_array_size(&v));
    EXPECT_EQ_INT(LEPT_NULL,   lept_get_type(lept_get_array_element(&v, 0)));
    EXPECT_EQ_INT(LEPT_FALSE,  lept_get_type(lept_get_array_element(&v, 1)));
    EXPECT_EQ_INT(LEPT_TRUE,   lept_get_type(lept_get_array_element(&v, 2)));
    EXPECT_EQ_INT(LEPT_NUMBER, lept_get_type(lept_get_array_element(&v, 3)));
    EXPECT_EQ_INT(LEPT_STRING, lept_get_type(lept_get_array_element(&v, 4)));
    EXPECT_EQ_DOUBLE(123.0, lept_get_number(lept_get_array_element(&v, 3)));
    EXPECT_EQ_STRING("abc", lept_get_string(lept_get_array_element(&v, 4)), lept_get_string_length(lept_get_array_element(&v, 4)));
    lept_free(&v);

    lept_init(&v);
    EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "[ [ ] , [ 0 ] , [ 0 , 1 ] , [ 0 , 1 , 2 ] ]"));
    EXPECT_EQ_INT(LEPT_ARRAY, lept_get_type(&v));
    EXPECT_EQ_SIZE_T(4, lept_get_array_size(&v));
    for (i = 0; i < 4; i++) {
        lept_value* a = lept_get_array_element(&v, i);
        EXPECT_EQ_INT(LEPT_ARRAY, lept_get_type(a));
        EXPECT_EQ_SIZE_T(i, lept_get_array_size(a));
        for (j = 0; j < i; j++) {
            lept_value* e = lept_get_array_element(a, j);
            EXPECT_EQ_INT(LEPT_NUMBER, lept_get_type(e));
            EXPECT_EQ_DOUBLE((double)j, lept_get_number(e));
        }
    }
    lept_free(&v);
}
Beispiel #9
0
static void testKnnEstimateQuery() {
  unsigned nObs;
  unsigned nDims;
  double *inputsP;
  double *targetsP;
  makeData(&nObs, &nDims, &inputsP, &targetsP);

  // query = [1 1]
  {
    double query[] = {1, 1};
    
    double estimate =
      Knn_estimate_query(nObs, nDims, inputsP, targetsP, 1, query);
    EXPECT_EQ_DOUBLE(1.0, estimate, 1e-10);
    
    estimate = Knn_estimate_query(nObs, nDims, inputsP, targetsP, 3, query);
    EXPECT_EQ_DOUBLE(1.0, estimate, 1e-10);
    
    estimate = Knn_estimate_query(nObs, nDims, inputsP, targetsP, 4, query);
    EXPECT_EQ_DOUBLE(1.5, estimate, 1e-10);
  }

  // query = [2 1]
  {
    double query[] = {1.5, 1};
    
    double estimate =
      Knn_estimate_query(nObs, nDims, inputsP, targetsP, 1, query);
    EXPECT_EQ_DOUBLE(1.0, estimate, 1e-10);
    
    estimate = Knn_estimate_query(nObs, nDims, inputsP, targetsP, 2, query);
    EXPECT_EQ_DOUBLE(1.5, estimate, 1e-10);
    
    estimate = Knn_estimate_query(nObs, nDims, inputsP, targetsP, 3, query);
    EXPECT_EQ_DOUBLE(1.0, estimate, 1e-10);

    estimate = Knn_estimate_query(nObs, nDims, inputsP, targetsP, 4, query);
    EXPECT_EQ_DOUBLE(1.5, estimate, 1e-10);
  }
}
Beispiel #10
0
static void testKnnReestimateQueryIndex() {
  const int trace = 0;
  unsigned nObs;
  unsigned nDims;
  double *inputsP;
  double *targetsP;
  makeData(&nObs, &nDims, &inputsP, &targetsP);

  // buid the cache
  KnnCache_T cache = KnnCache_new(nObs);
  const unsigned nNeighbors = 3;
  unsigned nnIndices[nNeighbors];

  for (unsigned queryIndex = 0; queryIndex < nObs; queryIndex++) {
    Knn_nearest_indices(nObs, nDims, inputsP, nNeighbors, nnIndices, queryIndex);
    KnnCache_set(cache, queryIndex, nNeighbors, nnIndices);
  }

  // re-estimate each target for k == 1
  {
    const unsigned k = 1;
    double expected[] = {1.0, 0.0, 1.0, 2.0}; // {1, 0 or 2, 1 or 3, 2}
    for (unsigned queryIndex = 0; queryIndex < nObs; queryIndex++) {
      double estimate = 
        Knn_reestimate_queryIndex(nObs, nDims, 
                                  inputsP, targetsP, 
                                  k, queryIndex, 
                                  nNeighbors, cache);
      if (trace) printf("queryIndex=%u\n", queryIndex);
      EXPECT_EQ_DOUBLE(expected[queryIndex], estimate, 1e-10);
    }
  }
  // re-estimate each target for k == 2
  {
    const unsigned k = 2;
    double expected[] = {1.5, 1.0, 2.0, 1.5};
    for (unsigned queryIndex = 0; queryIndex < nObs; queryIndex++) {
      double estimate = 
        Knn_reestimate_queryIndex(nObs, nDims, 
                                  inputsP, targetsP, 
                                  k, queryIndex, 
                                  nNeighbors, cache);
      if (trace) printf("queryIndex=%u\n", queryIndex);
      EXPECT_EQ_DOUBLE(expected[queryIndex], estimate, 1e-10);
    }
  }

  // re-estimate each target for k == 3
  {
    const unsigned k = 3;
    double expected[] = {2.0, 1.666666666667, 1.3333333333, 1.0};
    for (unsigned queryIndex = 0; queryIndex < nObs; queryIndex++) {
      double estimate = 
        Knn_reestimate_queryIndex(nObs, nDims, 
                                  inputsP, targetsP, 
                                  k, queryIndex, 
                                  nNeighbors, cache);
      if (trace) printf("queryIndex=%u\n", queryIndex);
      EXPECT_EQ_DOUBLE(expected[queryIndex], estimate, 1e-10);
    }
  }


    
}