Пример #1
0
Sawyer::Optional<BaseSemantics::SValuePtr>
SValue::createOptionalMerge(const BaseSemantics::SValuePtr &other_, const BaseSemantics::MergerPtr &merger,
                            const SmtSolverPtr &solver) const {
    SValuePtr other = SValue::promote(other_);
    SValuePtr retval = create_empty(other->get_width());
    bool changed = false;
    for (size_t i=0; i<subvalues.size(); ++i) {
        BaseSemantics::SValuePtr thisValue = subvalues[i];
        BaseSemantics::SValuePtr otherValue = other->subvalues[i];
        if (otherValue) {
            if (thisValue==NULL) {
                retval->subvalues.push_back(otherValue);
                changed = true;
            } else if (BaseSemantics::SValuePtr mergedValue =
                       thisValue->createOptionalMerge(otherValue, merger, solver).orDefault()) {
                changed = true;
                retval->subvalues.push_back(mergedValue);
            } else {
                retval->subvalues.push_back(thisValue);
            }
        } else {
            retval->subvalues.push_back(thisValue);
        }
    }
    return changed ? Sawyer::Optional<BaseSemantics::SValuePtr>(retval) : Sawyer::Nothing();
}
Пример #2
0
int main() 
{
    // create a source of data
    T data[ROWS][COLS] = {{1,2},{3,4}};

    // declare and initialize the matrices
    matrix  a,b;
    a = create_initvals(ROWS, COLS, data);
    b = create_empty(ROWS, COLS);

    // display the results of copying b to a
    printf("Setting b = a\n");
    equate(&a, &b);
    printf("\n Matrix a:");
    matrix_print(a);
    printf("\n Matrix b:");
    matrix_print(b);

    // display the results of adding a and b
    printf("\n a+b:");
    matrix_print(add(a,b));

    // display the results of transposing matrix a
    // After implementing the transpose function, uncomment the next line
    printf("\n a':");
    matrix_print(transpose(a));
    
    // If you created it, you destroy it
    destroy(a);
    destroy(b);

    return 0;
}
Пример #3
0
int
main(void)
{
    test_equal();

    test_overflow();

    lzma_index *i = create_empty();
    test_many(i);
    lzma_index_end(i, NULL);

    i = create_small();
    test_many(i);
    lzma_index_end(i, NULL);

    i = create_big();
    test_many(i);
    lzma_index_end(i, NULL);

    test_cat();

    test_locate();

    test_corrupt();

    return 0;
}
Пример #4
0
int main()
{
    T data[ROWS][COLS] = {{1,2},{3,4}};
    matrix  a,b;
    a = create_initvals(ROWS, COLS, data);
    b = create_empty(ROWS, COLS);
    printf("Setting b = a\n");
    equate(&a,&b);
    printf("\n Matrix a:");
    matrix_print(a);
    printf("\n Matrix b:");
    matrix_print(b);
    printf("\n a+b:");
    matrix_print(add(a,b));

    // After implementing the transpose function, uncomment the next line
    printf("\n a':");
    matrix_print(transpose(a));

    // If you created it, you destroy it
    destroy(a);
    destroy(b);

    return 0;
}
Пример #5
0
matrix scalar_multiply(T scalar, matrix m)
{
  int i,j;
  matrix result;
  result = create_empty(m.row_dim, m.col_dim);
  for (i=0; i<m.row_dim; i++)
    for (j=0; j<m.col_dim; j++)
      result.element[i][j] = scalar * m.element[i][j];
  return result; 
}
Пример #6
0
matrix negate(matrix m)
{
  int i,j;
  matrix result;
  result = create_empty(m.row_dim, m.col_dim);
  for (i=0; i<m.row_dim; i++)
    for (j=0; j<m.col_dim; j++)
      result.element[i][j] = -m.element[i][j];
  return result; 
}
Пример #7
0
matrix subtract(matrix m1, matrix m2)
{
  int i,j;
  matrix result;
  assert((m1.row_dim == m2.row_dim) && (m1.col_dim == m2.col_dim));
  result = create_empty(m1.row_dim, m2.col_dim);
  for (i=0; i<m1.row_dim; i++)
    for (j=0; j<m1.col_dim; j++)
      result.element[i][j] = m1.element[i][j] - m2.element[i][j];
  return result; 
}
int
main(void)
{
	test_equal();

	test_overflow();

	lzma_index *i = create_empty();
	test_many(i);
	lzma_index_end(i, NULL);

	i = create_small();
	test_many(i);
	lzma_index_end(i, NULL);

	i = create_big();
	test_many(i);
	lzma_index_end(i, NULL);

	test_cat();

	test_locate();

	test_corrupt();

	// Test for the bug fix 21515d79d778b8730a434f151b07202d52a04611:
	// liblzma: Fix lzma_index_dup() for empty Streams.
	i = create_empty();
	expect(lzma_index_stream_padding(i, 4) == LZMA_OK);
	test_copy(i);
	lzma_index_end(i, NULL);

	// Test for the bug fix 3bf857edfef51374f6f3fffae3d817f57d3264a0:
	// liblzma: Fix a memory leak in error path of lzma_index_dup().
	// Use Valgrind to see that there are no leaks.
	i = create_small();
	expect(lzma_index_dup(i, &my_allocator) == NULL);
	lzma_index_end(i, NULL);

	return 0;
}
Пример #9
0
static void
test_overflow(void)
{
    // Integer overflow tests
    lzma_index *i = create_empty();

    expect(lzma_index_append(i, NULL, LZMA_VLI_MAX - 5, 1234)
           == LZMA_DATA_ERROR);

    // TODO

    lzma_index_end(i, NULL);
}
Пример #10
0
matrix multiply(matrix m1, matrix m2)
{
  int i,j,k;
  matrix result;
  assert(m1.col_dim == m2.row_dim);
  result = create_empty(m1.row_dim, m2.col_dim);
  for (i=0; i<m1.row_dim; i++)
    for (j=0; j<m2.col_dim; j++) {
      result.element[i][j] = 0;
      for (k=0; k<m1.col_dim; k++)
        result.element[i][j] += m1.element[i][k] * m2.element[k][j];
    }
  return result; 
}
Пример #11
0
int main()
{
    static T data[] = {1,2,3,4,5,6,7,8,9,10,11,12};
    matrix  a,b;
    a = create_initvals(4,3,data);
    b = create_empty(4,3);
    equate(&a,&b);
    printf("\n Matrix a:");
    matrix_print(a);
    printf("\n Matrix b:");
    matrix_print(b);
    printf("\n a+b:");
    matrix_print(add(a,b));
    printf("\n a transposed:");
    matrix_print(transpose(a));
    printf("\n a+b transposed:");
    matrix_print(transpose(add(a,b)));
    return 0;
}
Пример #12
0
static void
test_equal(void)
{
    lzma_index *a = create_empty();
    lzma_index *b = create_small();
    lzma_index *c = create_big();
    expect(a && b && c);

    expect(is_equal(a, a));
    expect(is_equal(b, b));
    expect(is_equal(c, c));

    expect(!is_equal(a, b));
    expect(!is_equal(a, c));
    expect(!is_equal(b, c));

    lzma_index_end(a, NULL);
    lzma_index_end(b, NULL);
    lzma_index_end(c, NULL);
}
Пример #13
0
static void
test_corrupt(void)
{
    const size_t alloc_size = 128 * 1024;
    uint8_t *buf = malloc(alloc_size);
    expect(buf != NULL);
    lzma_stream strm = LZMA_STREAM_INIT;

    lzma_index *i = create_empty();
    expect(lzma_index_append(i, NULL, 0, 1) == LZMA_PROG_ERROR);
    lzma_index_end(i, NULL);

    // Create a valid Index and corrupt it in different ways.
    i = create_small();
    expect(lzma_index_encoder(&strm, i) == LZMA_OK);
    succeed(coder_loop(&strm, NULL, 0, buf, 20,
                       LZMA_STREAM_END, LZMA_RUN));
    lzma_index_end(i, NULL);

    // Wrong Index Indicator
    buf[0] ^= 1;
    expect(lzma_index_decoder(&strm, &i, MEMLIMIT) == LZMA_OK);
    succeed(decoder_loop_ret(&strm, buf, 1, LZMA_DATA_ERROR));
    buf[0] ^= 1;

    // Wrong Number of Records and thus CRC32 fails.
    --buf[1];
    expect(lzma_index_decoder(&strm, &i, MEMLIMIT) == LZMA_OK);
    succeed(decoder_loop_ret(&strm, buf, 10, LZMA_DATA_ERROR));
    ++buf[1];

    // Padding not NULs
    buf[15] ^= 1;
    expect(lzma_index_decoder(&strm, &i, MEMLIMIT) == LZMA_OK);
    succeed(decoder_loop_ret(&strm, buf, 16, LZMA_DATA_ERROR));

    lzma_end(&strm);
    free(buf);
}
Пример #14
0
static void
test_cat(void)
{
    lzma_index *a, *b, *c;
    lzma_index_iter r;

    // Empty Indexes
    a = create_empty();
    b = create_empty();
    expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
    expect(lzma_index_block_count(a) == 0);
    expect(lzma_index_stream_size(a) == 2 * LZMA_STREAM_HEADER_SIZE + 8);
    expect(lzma_index_file_size(a)
           == 2 * (2 * LZMA_STREAM_HEADER_SIZE + 8));
    lzma_index_iter_init(&r, a);
    expect(lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK));

    b = create_empty();
    expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
    expect(lzma_index_block_count(a) == 0);
    expect(lzma_index_stream_size(a) == 2 * LZMA_STREAM_HEADER_SIZE + 8);
    expect(lzma_index_file_size(a)
           == 3 * (2 * LZMA_STREAM_HEADER_SIZE + 8));

    b = create_empty();
    c = create_empty();
    expect(lzma_index_stream_padding(b, 4) == LZMA_OK);
    expect(lzma_index_cat(b, c, NULL) == LZMA_OK);
    expect(lzma_index_block_count(b) == 0);
    expect(lzma_index_stream_size(b) == 2 * LZMA_STREAM_HEADER_SIZE + 8);
    expect(lzma_index_file_size(b)
           == 2 * (2 * LZMA_STREAM_HEADER_SIZE + 8) + 4);

    expect(lzma_index_stream_padding(a, 8) == LZMA_OK);
    expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
    expect(lzma_index_block_count(a) == 0);
    expect(lzma_index_stream_size(a) == 2 * LZMA_STREAM_HEADER_SIZE + 8);
    expect(lzma_index_file_size(a)
           == 5 * (2 * LZMA_STREAM_HEADER_SIZE + 8) + 4 + 8);

    expect(lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK));
    lzma_index_iter_rewind(&r);
    expect(lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK));
    lzma_index_end(a, NULL);

    // Small Indexes
    a = create_small();
    lzma_vli stream_size = lzma_index_stream_size(a);
    lzma_index_iter_init(&r, a);
    for (int i = SMALL_COUNT; i >= 0; --i)
        expect(!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK)
               ^ (i == 0));

    b = create_small();
    expect(lzma_index_stream_padding(a, 4) == LZMA_OK);
    expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
    expect(lzma_index_file_size(a) == stream_size * 2 + 4);
    expect(lzma_index_stream_size(a) > stream_size);
    expect(lzma_index_stream_size(a) < stream_size * 2);
    for (int i = SMALL_COUNT; i >= 0; --i)
        expect(!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK)
               ^ (i == 0));

    lzma_index_iter_rewind(&r);
    for (int i = SMALL_COUNT * 2; i >= 0; --i)
        expect(!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK)
               ^ (i == 0));

    b = create_small();
    c = create_small();
    expect(lzma_index_stream_padding(b, 8) == LZMA_OK);
    expect(lzma_index_cat(b, c, NULL) == LZMA_OK);
    expect(lzma_index_stream_padding(a, 12) == LZMA_OK);
    expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
    expect(lzma_index_file_size(a) == stream_size * 4 + 4 + 8 + 12);

    expect(lzma_index_block_count(a) == SMALL_COUNT * 4);
    for (int i = SMALL_COUNT * 2; i >= 0; --i)
        expect(!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK)
               ^ (i == 0));

    lzma_index_iter_rewind(&r);
    for (int i = SMALL_COUNT * 4; i >= 0; --i)
        expect(!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK)
               ^ (i == 0));

    lzma_index_end(a, NULL);

    // Mix of empty and small
    a = create_empty();
    b = create_small();
    expect(lzma_index_stream_padding(a, 4) == LZMA_OK);
    expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
    lzma_index_iter_init(&r, a);
    for (int i = SMALL_COUNT; i >= 0; --i)
        expect(!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK)
               ^ (i == 0));

    lzma_index_end(a, NULL);

    // Big Indexes
    a = create_big();
    stream_size = lzma_index_stream_size(a);
    b = create_big();
    expect(lzma_index_stream_padding(a, 4) == LZMA_OK);
    expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
    expect(lzma_index_file_size(a) == stream_size * 2 + 4);
    expect(lzma_index_stream_size(a) > stream_size);
    expect(lzma_index_stream_size(a) < stream_size * 2);

    b = create_big();
    c = create_big();
    expect(lzma_index_stream_padding(b, 8) == LZMA_OK);
    expect(lzma_index_cat(b, c, NULL) == LZMA_OK);
    expect(lzma_index_stream_padding(a, 12) == LZMA_OK);
    expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
    expect(lzma_index_file_size(a) == stream_size * 4 + 4 + 8 + 12);

    lzma_index_iter_init(&r, a);
    for (int i = BIG_COUNT * 4; i >= 0; --i)
        expect(!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK)
               ^ (i == 0));

    lzma_index_end(a, NULL);
}