Esempio n. 1
0
void usart_begin(usart * usx, uint32_t baud) {
	USART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization
	NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller)

	USART_InitStruct.USART_BaudRate = baud;	// the baudrate is set to the value we passed into this init function
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;// we want the data frame size to be 8 bits (standard)
	USART_InitStruct.USART_StopBits = USART_StopBits_1;	// we want 1 stop bit (standard)
	USART_InitStruct.USART_Parity = USART_Parity_No;// we don't want a parity bit (standard)
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
	USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // we want to enable the transmitter and the receiver

	USART_Init(usx->USARTx, &USART_InitStruct); // again all the properties are passed to the USART_Init function which takes care of all the bit setting

	USART_ITConfig(usx->USARTx, USART_IT_RXNE, ENABLE); // enable the USART3 receive interrupt
	USART_ITConfig(usx->USARTx, USART_IT_TXE, DISABLE);

	NVIC_InitStructure.NVIC_IRQChannel = usx->irqn;
	// we want to configure the USART3 interrupts
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; // this sets the priority group of the USART interrupts
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // this sets the subpriority inside the group
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;	// the USART interrupts are globally enabled
	NVIC_Init(&NVIC_InitStructure);	// the properties are passed to the NVIC_Init function which takes care of the low level stuff
	//
	ring_clear(&usx->rxring); //rxring[usx->usid]);
	ring_clear(&usx->txring); //txring[usx->usid]);
	// finally this enables the complete USART peripheral
	USART_Cmd(usx->USARTx, ENABLE);
}
Esempio n. 2
0
/** Clears the buart's transmit and receive buffers.  */
void
buart_clear (buart_t buart)
{
    buart_dev_t *dev = buart;

    ring_clear (&dev->rx_ring);
    ring_clear (&dev->tx_ring);
}
Esempio n. 3
0
/*
 * 清除串口接收到的数据
 */
static void serial_clear(urg_serial_t* serial)
{
    tcdrain(serial->fd);
    tcflush(serial->fd, TCIOFLUSH);
    ring_clear(&serial->ring);
    serial->has_last_ch = False;
}
Esempio n. 4
0
void serial_clear(serial_t* serial)
{
  tcdrain(serial->fd_);
  tcflush(serial->fd_, TCIOFLUSH);
  ring_clear(&serial->ring_);
  serial->has_last_ch_ = False;
}
Esempio n. 5
0
int8_t GSwifi::parseHead2(uint8_t dat, int8_t cid) {
    if (dat == '\n') {
        continuous_newlines_ ++;
    }
    else if (dat == '\r') {
        // preserve
    }
    else {
        continuous_newlines_ = 0;
        if ( ! ring_isfull(_buf_cmd) ) {
            // ignore if overflowed
            ring_put( _buf_cmd, dat );
        }
    }
    if (continuous_newlines_ == 1) {
        // check "Content-Length: x" header
        // if it's non 0, wait for more body data
        // otherwise disconnect after handling response
        // GS splits HTTP requests/responses into multiple bulk messages,
        // 1st ends just after headers, and 2nd contains only response body
        // "Content-Length: "    .length = 16
        // "Content-Length: 9999".length = 20
        // "X-Requested-With: "  .length = 18
        char content_length_chars[21];
        memset( content_length_chars, 0, 21 );

        uint8_t copied = ring_get( _buf_cmd, content_length_chars, 20 );
        if ((copied >= 16) &&
            (strncmp(content_length_chars, "Content-Length: ", 16) == 0)) {
            content_length_chars[20] = 0;
            content_lengths_[ cid ] = atoi(&content_length_chars[16]);
        }
        if ((copied >= 18) &&
            (strncmp(content_length_chars, "X-Requested-With: ", 18) == 0)) {
            has_requested_with_ = true;
        }
        ring_clear(_buf_cmd);
    }
    if (continuous_newlines_ == 2) {
        // if detected double (\r)\n, switch to body mode
        ring_clear(_buf_cmd);
        return 0;
    }
    return -1;
}
Esempio n. 6
0
void HardwareSerialX::end()
{
  // wait for transmission of outgoing data
  while ( ! ring_isempty( _tx_buffer ) ) ;

  cbi(*_ucsrb, _rxen);
  cbi(*_ucsrb, _txen);
  cbi(*_ucsrb, _rxcie);
  cbi(*_ucsrb, _udrie);

  // clear any received data
  ring_clear( _rx_buffer );
}
Esempio n. 7
0
void GSwifi::clear () {
    joined_         = false;
    listening_      = false;
    limited_ap_     = false;
    resetResponse(GSCOMMANDMODE_NONE);
    gs_mode_        = GSMODE_COMMAND;
    ring_clear(_buf_cmd);
    memset(ipaddr_, 0, sizeof(ipaddr_));

    for (uint8_t i=0; i<16; i++) {
        TIMER_STOP( timers_[i] );
    }
}
Esempio n. 8
0
void usart_flush(usart * usx) {
	uint32_t wtill = millis() + 100;
	while (ring_count(&usx->txring) > 0) {
		if (millis() > wtill)
			break;
	}
	/*
	 USART_ITConfig(usx->USARTx, USART_IT_RXNE, DISABLE); // disable the USART3 receive interrupt
	 buffer_clear(usx->rxring); //&rxring[usx->usid]);
	 USART_ClearITPendingBit(usx->USARTx, USART_IT_RXNE );
	 USART_ITConfig(usx->USARTx, USART_IT_RXNE, ENABLE); // enable the USART3 receive interrupt
	 USART_ITConfig(usx->USARTx, USART_IT_TXE, DISABLE);
	 while ( buffer_count(usx->txring //&txring[usx->usid]
	 ) > 0 ) {
	 while (USART_GetFlagStatus(usx->USARTx, USART_FLAG_TXE ) == RESET);
	 USART_SendData(usx->USARTx, buffer_deque(usx->txring)); //&txring[usx->usid]));
	 while (USART_GetFlagStatus(usx->USARTx, USART_FLAG_TC ) == RESET);
	 }
	 USART_ClearITPendingBit(usx->USARTx, USART_IT_TXE );
	 */
	ring_clear(&usx->rxring); //&txring[usx->usid]);
}
Esempio n. 9
0
void ring_write(Ring *ring, unsigned char *buf, unsigned int size) {
  ring_clear(ring, sizeof(int) + size);
  ring_raw_write(ring, ring->write_index, (unsigned char*) &size, sizeof(int));
  ring_raw_write(ring, ring->write_index + sizeof(int), buf, size);
  ring->write_index += sizeof(int) + size;
}
Esempio n. 10
0
void ring_initialize(ring_buffer_t *ring, char *buffer, const int shift_length)
{
    ring->buffer = buffer;
    ring->buffer_size = 1 << shift_length;
    ring_clear(ring);
}
Esempio n. 11
0
/* ------------------------------------------------------------------
 * Drop the content of the rx buffer
 * ------------------------------------------------------------------ */
void uart_rx_clear() {
	ring_clear(&rx);
}
Esempio n. 12
0
/* ------------------------------------------------------------------
 * Drop the content of the tx buffer
 * ------------------------------------------------------------------ */
void uart_tx_clear() {
	ring_clear(&tx);
}
Esempio n. 13
0
int main()
{
    flint_rand_t state;
    long i;

    printf("divexact....");
    fflush(stdout);

    flint_randinit(state);

    /* polynomials over (fmpz) integers */
    {
        ring_t Z, Zx, Zxy, Zxyz;
        long size[4] = {6, 6, 6, 6};

        ring_init_fmpz(Z);
        ring_init_poly(Zx, Z);
        ring_init_poly(Zxy, Zx);
        ring_init_poly(Zxyz, Zxy);

        test_divexact(state, Z, size, 1000);
        test_divexact(state, Zx, size, 1000);
        test_divexact(state, Zxy, size, 1000);
        test_divexact(state, Zxyz, size, 1000);

        ring_clear(Zxyz);
        ring_clear(Zxy);
        ring_clear(Zx);
        ring_clear(Z);
    }

    /* polynomials over (fmpz) integers mod n */
    for (i = 0; i < 100; i++)
    {
        ring_t Z, Zn, Znx, Znxy, Znxyz;
        fmpz_t mod;
        long size[4] = {6, 6, 6, 6};

        ring_init_fmpz(Z);

        fmpz_init(mod);
        fmpz_set_ui(mod, n_randtest_prime(state, 0));
        ring_init_mod(Zn, Z, mod);

        ring_init_poly(Znx, Zn);
        ring_init_poly(Znxy, Znx);
        ring_init_poly(Znxyz, Znxy);

        test_divexact(state, Zn, size, 10);
        test_divexact(state, Znx, size, 10);
        test_divexact(state, Znxy, size, 10);
        test_divexact(state, Znxyz, size, 10);

        ring_clear(Znxyz);
        ring_clear(Znxy);
        ring_clear(Znx);
        ring_clear(Zn);
        fmpz_clear(mod);
        ring_clear(Z);
    }

    /* polynomials over (nmod) integers mod n */
    for (i = 0; i < 100; i++)
    {
        ring_t Z, Zn, Znx, Znxy, Znxyz;
        mp_limb_t mod;
        long size[4] = {6, 6, 6, 6};

        ring_init_limb(Z);
        mod = n_randtest_prime(state, 0);
        ring_init_mod(Zn, Z, &mod);

        ring_init_poly(Znx, Zn);
        ring_init_poly(Znxy, Znx);
        ring_init_poly(Znxyz, Znxy);

        test_divexact(state, Zn, size, 10);
        test_divexact(state, Znx, size, 10);
        test_divexact(state, Znxy, size, 10);
        test_divexact(state, Znxyz, size, 10);

        ring_clear(Znxyz);
        ring_clear(Znxy);
        ring_clear(Znx);
        ring_clear(Zn);
        ring_clear(Z);
    }

    /* polynomials over (fmpz) integer fractions */
    {
        ring_t Z, Zx, Zxy, Zq, Zqx, Zxq, Zqxy, Zxqy, Zxyq;
        long size[4] = {6, 6, 6, 6};

        ring_init_fmpz(Z);
        ring_init_poly(Zx, Z);
        ring_init_poly(Zxy, Zx);
        ring_init_frac(Zq, Z, Z);
        ring_init_poly(Zqx, Zq);
        ring_init_frac(Zxq, Zx, Z);
        ring_init_poly(Zqxy, Zqx);
        ring_init_poly(Zxqy, Zxy);
        ring_init_frac(Zxyq, Zxy, Z);

        test_divexact(state, Zq, size, 1000);
        test_divexact(state, Zqx, size, 1000);
        test_divexact(state, Zxq, size, 1000);
        test_divexact(state, Zqxy, size, 1000);
        test_divexact(state, Zxqy, size, 1000);

        /*  not yet supported:
        test_divexact(state, Zxyq, size, 1000);
        */

        ring_clear(Zxyq);
        ring_clear(Zxqy);
        ring_clear(Zqxy);
        ring_clear(Zxq);
        ring_clear(Zqx);
        ring_clear(Zq);
        ring_clear(Zxy);
        ring_clear(Zx);
        ring_clear(Z);
    }

    /* Complex numbers */
    {
        ring_t Z, Zx, Zi, Zxi, Zix, Zq, Zqi;
        long size[4] = {6, 6, 6, 6};

        ring_init_fmpz(Z);
        ring_init_poly(Zx, Z);
        ring_init_complex(Zi, Z);
        ring_init_complex(Zxi, Zx);
        ring_init_poly(Zix, Zi);
        ring_init_frac(Zq, Z, Z);
        ring_init_complex(Zqi, Zq);

        test_divexact(state, Zi, size, 1000);
        test_divexact(state, Zqi, size, 1000);

/*
        not yet supported
        test_divexact(state, Zxi, size, 1000);
        test_divexact(state, Zix, size, 1000);
*/

        ring_clear(Zqi);
        ring_clear(Zq);
        ring_clear(Zix);
        ring_clear(Zxi);
        ring_clear(Zi);
        ring_clear(Zx);
        ring_clear(Z);
    }

    flint_randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return EXIT_SUCCESS;
}
Esempio n. 14
0
void tcpclient_buffer_flush(tcpclient_t* cli)
{
    ring_clear(&cli->rb);
}
Esempio n. 15
0
// received a character from UART
void GSwifi::parseByte(uint8_t dat) {
    static uint8_t  next_token; // split each byte into tokens (cid,ip,port,length,data)
    static bool     escape = false;
    char temp[GS_MAX_PATH_LENGTH+1];

    if (dat == ESCAPE) {
        // 0x1B : Escape
        GSLOG_PRINT("e< ");
    }
    else { // if (next_token != NEXT_TOKEN_DATA) {
        GSLOG_WRITE(dat);
    }

    if (gs_mode_ == GSMODE_COMMAND) {
        if (escape) {
            // esc
            switch (dat) {
            case 'O':
            case 'F':
                // ignore
                break;
            case 'Z':
            case 'H':
                gs_mode_   = GSMODE_DATA_RX_BULK;
                next_token = NEXT_TOKEN_CID;
                break;
            default:
                // GSLOG_PRINT("!E1 "); GSLOG_PRINTLN2(dat,HEX);
                break;
            }
            escape = false;
        }
        else {
            if (dat == ESCAPE) {
                escape = true;
            }
            else if (dat == '\n') {
                // end of line
                parseLine();
            }
            else if (dat != '\r') {
                if ( ! ring_isfull(_buf_cmd) ) {
                    ring_put(_buf_cmd, dat);
                }
                else {
                    GSLOG_PRINTLN("!E2");
                }
            }
        }
        return;
    }
    else if (gs_mode_ != GSMODE_DATA_RX_BULK) {
        return;
    }

    static uint16_t       len;
    static char           len_chars[5];
    static int8_t         current_cid;
    static GSREQUESTSTATE request_state;

    if (next_token == NEXT_TOKEN_CID) {
        // dat is cid
        current_cid = x2i(dat);
        ASSERT((0 <= current_cid) && (current_cid <= 16));

        next_token  = NEXT_TOKEN_LENGTH;
        len         = 0;
    }
    else if (next_token == NEXT_TOKEN_LENGTH) {
        // Data Length is 4 ascii char represents decimal value i.e. 1400 byte (0x31 0x34 0x30 0x30)
        len_chars[ len ++ ] = dat;
        if (len >= 4) {
            len_chars[ len ] = 0;
            len        = atoi(len_chars); // length of data
            next_token = NEXT_TOKEN_DATA;

            if (content_lengths_[ current_cid ] > 0) {
                // this is our 2nd bulk message from GS for this response
                // we already swallowed HTTP response headers,
                // following should be body
                request_state = GSREQUESTSTATE_BODY;
            }
            else {
                request_state = GSREQUESTSTATE_HEAD1;
            }
            ring_clear( _buf_cmd ); // reuse _buf_cmd to store HTTP request
        }
    }
    else if (next_token == NEXT_TOKEN_DATA) {
        len --;

        if (cidIsRequest(current_cid)) { // request against us
            static uint16_t error_code;
            static int8_t   routeid;

            switch (request_state) {
            case GSREQUESTSTATE_HEAD1:
                if (dat != '\n') {
                    if ( ! ring_isfull(_buf_cmd) ) {
                        ring_put( _buf_cmd, dat );
                    }
                    // ignore overflowed
                }
                else {
                    // end of request line

                    // reuse "temp" buffer to parse method and path
                    int8_t  result  = parseRequestLine((char*)temp, 7);
                    GSMETHOD method = GSMETHOD_UNKNOWN;
                    if ( result == 0 ) {
                        method = x2method(temp);
                        result = parseRequestLine((char*)temp, GS_MAX_PATH_LENGTH);
                    }
                    if ( result != 0 ) {
                        // couldn't detect method or path
                        request_state = GSREQUESTSTATE_ERROR;
                        error_code    = 400;
                        ring_clear(_buf_cmd);
                        break;
                    }

                    routeid = router(method, temp);
                    if ( routeid < 0 ) {
                        request_state = GSREQUESTSTATE_ERROR;
                        error_code    = 404;
                        ring_clear(_buf_cmd);
                        break;
                    }
                    request_state                   = GSREQUESTSTATE_HEAD2;
                    continuous_newlines_            = 0;
                    content_lengths_[ current_cid ] = 0;
                    has_requested_with_             = false;
                    ring_clear(_buf_cmd);
                }
                break;
            case GSREQUESTSTATE_HEAD2:
                if(0 == parseHead2(dat, current_cid)) {
                    request_state = GSREQUESTSTATE_BODY;
                    // dispatched once, at start of body
                    dispatchRequestHandler(current_cid, routeid, GSREQUESTSTATE_BODY_START);
                }
                break;
            case GSREQUESTSTATE_BODY:
                if (content_lengths_[ current_cid ] > 0) {
                    content_lengths_[ current_cid ] --;
                }
                if (ring_isfull(_buf_cmd)) {
                    dispatchRequestHandler(current_cid, routeid, request_state); // POST, user callback should write()
                }
                ring_put(_buf_cmd, dat);
                break;
            case GSREQUESTSTATE_ERROR:
                // skip until received whole request
                break;
            case GSREQUESTSTATE_RECEIVED:
            default:
                break;
            }

            // end of bulk transfered data
            if (len == 0) {
                gs_mode_ = GSMODE_COMMAND;

                if ( request_state == GSREQUESTSTATE_ERROR ) {
                    writeHead( current_cid, error_code );
                    writeEnd();
                    ring_put( &commands, COMMAND_CLOSE );
                    ring_put( &commands, current_cid );
                }
                else {
                    if (content_lengths_[ current_cid ] == 0) {
                        // if Content-Length header was longer than <ESC>Z length,
                        // we wait til next bulk transfer
                        request_state = GSREQUESTSTATE_RECEIVED;
                    }
                    // user callback should write(), writeEnd() and close()
                    dispatchRequestHandler(current_cid, routeid, request_state);
                }
                ring_clear(_buf_cmd);
            }
        }
        else {
            // is request from us
            static uint16_t status_code;

            switch (request_state) {
            case GSREQUESTSTATE_HEAD1:
                if (dat != '\n') {
                    if ( ! ring_isfull(_buf_cmd) ) {
                        // ignore if overflowed
                        ring_put( _buf_cmd, dat );
                    }
                }
                else {
                    uint8_t i=0;

                    // skip 9 characters "HTTP/1.1 "
                    while (i++ < 9) {
                        ring_get( _buf_cmd, &temp[0], 1 );
                    }

                    // copy 3 numbers representing status code into temp buffer
                    temp[ 3 ] = 0;
                    int8_t count = ring_get( _buf_cmd, temp, 3 );
                    if (count != 3) {
                        // protocol error
                        // we should receive something like: "200 OK", "401 Unauthorized"
                        status_code   = 999;
                        request_state = GSREQUESTSTATE_ERROR;
                        break;
                    }
                    status_code                     = atoi(temp);
                    request_state                   = GSREQUESTSTATE_HEAD2;
                    continuous_newlines_            = 0;
                    content_lengths_[ current_cid ] = 0;
                    ring_clear(_buf_cmd);
                }
                break;
            case GSREQUESTSTATE_HEAD2:
                if(0 == parseHead2(dat, current_cid)) {
                    request_state = GSREQUESTSTATE_BODY;
                    // dispatched once, at start of body
                    dispatchResponseHandler(current_cid, status_code, GSREQUESTSTATE_BODY_START);
                }
                break;
            case GSREQUESTSTATE_BODY:
                if (content_lengths_[ current_cid ] > 0) {
                    content_lengths_[ current_cid ] --;
                }
                if (ring_isfull(_buf_cmd)) {
                    dispatchResponseHandler(current_cid, status_code, GSREQUESTSTATE_BODY);
                }
                ring_put(_buf_cmd, dat);
                break;
            case GSREQUESTSTATE_ERROR:
            case GSREQUESTSTATE_RECEIVED:
            default:
                break;
            }

            if (len == 0) {
                gs_mode_ = GSMODE_COMMAND;

                if ( request_state == GSREQUESTSTATE_ERROR ) {
                    dispatchResponseHandler(current_cid, status_code, request_state);
                }
                else {
                    if (content_lengths_[ current_cid ] == 0) {
                        // if Content-Length header was longer than <ESC>Z length,
                        // we wait til all response body received.
                        // we need to close our clientRequest before handling it.
                        // GS often locks when closing 2 connections in a row
                        // ex: POST /keys from iPhone (cid:1) -> POST /keys to server (cid:2)
                        //     response from server arrives -> close(1) -> close(2) -> lock!!
                        // the other way around: close(2) -> close(1) doesn't lock :(
                        request_state = GSREQUESTSTATE_RECEIVED;
                    }
                    dispatchResponseHandler(current_cid, status_code, request_state);
                }
                ring_clear( _buf_cmd );
            }
        } // is response
    } // (next_token == NEXT_TOKEN_DATA)
}
Esempio n. 16
0
int main() {
    ok( 1, "ok" );

    {
        struct RingBuffer buf_;
        struct RingBuffer *buf = &buf_;
        char data[65];
        ring_init( buf, data, 65 );
        ok( ring_used(buf) == 0, "0 used" );
        ok( ring_isempty(buf) == 1, "is empty" );

        ring_put(buf, 'a');
        ok( ring_used(buf) == 1, "1 used after put" );
        ok( ring_isfull(buf) == 0, "not full" );
        ok( ring_isempty(buf) == 0, "is empty" );

        char buf2[64];
        ring_get(buf, &buf2[0], 1);
        ok( buf2[0] == 'a', "get" );
        ok( ring_used(buf) == 0, "0 used after get" );
        ok( ring_isfull(buf) == 0, "not full" );
        ok( ring_isempty(buf) == 1, "is empty" );

        ring_put(buf, 'b');
        ring_clear(buf);

        ok( ring_used(buf) == 0, "0 used after clear" );
        ok( ring_isfull(buf) == 0, "not full" );
        ok( ring_isempty(buf) == 1, "is empty" );

        ring_put(buf, '0');        ring_put(buf, '1');        ring_put(buf, '2');        ring_put(buf, '3');
        ring_put(buf, '4');        ring_put(buf, '5');        ring_put(buf, '6');        ring_put(buf, '7');
        ring_put(buf, '8');        ring_put(buf, '9');        ring_put(buf, 'a');        ring_put(buf, 'b');
        ring_put(buf, 'c');        ring_put(buf, 'd');        ring_put(buf, 'e');        ring_put(buf, 'f');
        ring_put(buf, '0');        ring_put(buf, '1');        ring_put(buf, '2');        ring_put(buf, '3');
        ring_put(buf, '4');        ring_put(buf, '5');        ring_put(buf, '6');        ring_put(buf, '7');
        ring_put(buf, '8');        ring_put(buf, '9');        ring_put(buf, 'a');        ring_put(buf, 'b');
        ring_put(buf, 'c');        ring_put(buf, 'd');        ring_put(buf, 'e');        ring_put(buf, 'f');
        ring_put(buf, '0');        ring_put(buf, '1');        ring_put(buf, '2');        ring_put(buf, '3');
        ring_put(buf, '4');        ring_put(buf, '5');        ring_put(buf, '6');        ring_put(buf, '7');
        ring_put(buf, '8');        ring_put(buf, '9');        ring_put(buf, 'a');        ring_put(buf, 'b');
        ring_put(buf, 'c');        ring_put(buf, 'd');        ring_put(buf, 'e');        ring_put(buf, 'f');
        ring_put(buf, '0');        ring_put(buf, '1');        ring_put(buf, '2');        ring_put(buf, '3');
        ring_put(buf, '4');        ring_put(buf, '5');        ring_put(buf, '6');        ring_put(buf, '7');
        ring_put(buf, '8');        ring_put(buf, '9');        ring_put(buf, 'a');        ring_put(buf, 'b');
        ring_put(buf, 'c');        ring_put(buf, 'd');        ring_put(buf, 'e');        ring_put(buf, 'f');
        ok( ring_used(buf) == 64, "64 used after 64 puts" );
        ok( ring_isfull(buf) == 1, "is full" );
        ok( ring_isempty(buf) == 0, "is empty" );

        // dropped feature to protect buffer from overflow
        // ok( ring_put(buf, 'x') == -1, "can't put into full" );

        uint8_t fetched = ring_get(buf, &buf2[0], 64);
        ok( buf2[0] == '0', "get 1" );
        ok( buf2[1] == '1', "get 2" );
        ok( buf2[2] == '2', "get 3" );
        ok( buf2[3] == '3', "get 4" );
        ok( fetched == 64, "fetched all" );

        ok( ring_used(buf) == 0, "0 used after all get" );
        ok( ring_isfull(buf) == 0, "not full again" );
        ok( ring_isempty(buf) == 1, "is empty" );
    }

    done_testing();
}
int main()
{
    flint_rand_t state;
    long iter;

    printf("poly_divrem_basecase....");
    fflush(stdout);

    flint_randinit(state);

    for (iter = 0; iter < 10000; iter++)
    {
        ring_t ZZ, ZZp, ZZpx[3];
        ring_struct * ring;
        elem_ptr p;
        long size[3];
        elem_poly_t A, B, C, Q, Q2, R, R2;

        ring_init_limb(ZZ);
        ELEM_TMP_INIT(p, ZZ);
        elem_set_ui(p, n_randtest_prime(state, 0), ZZ);
        ring_init_mod(ZZp, ZZ, p);
        ring_init_poly(ZZpx[0], ZZp);
        ring_init_poly(ZZpx[1], ZZpx[0]);
        ring_init_poly(ZZpx[2], ZZpx[1]);

        ring = ZZpx[n_randint(state, 2)];

        size[0] = 1 + n_randint(state, 30);
        size[1] = 1 + n_randint(state, 30);
        size[2] = 1 + n_randint(state, 30);

        elem_init(A, ring);
        elem_init(B, ring);
        elem_init(C, ring);
        elem_init(Q, ring);
        elem_init(Q2, ring);
        elem_init(R, ring);
        elem_init(R2, ring);

        elem_randtest(A, state, size, ring);
        elem_randtest_not_zero(B, state, size, ring);
        elem_mul(C, A, B, ring);

        elem_poly_divrem_basecase(Q, R, C, B, ring);
        if (!elem_equal(Q, A, ring) || !elem_is_zero(R, ring))
        {
            printf("FAIL: (A * B) / B = A\n");
            elem_print(A, ring); printf("\n\n");
            elem_print(B, ring); printf("\n\n");
            elem_print(C, ring); printf("\n\n");
            elem_print(Q, ring); printf("\n\n");
            elem_print(R, ring); printf("\n\n");
            abort();
        }

        elem_divrem(Q, R, A, B, ring);
        elem_mul(C, Q, B, ring);
        elem_add(C, C, R, ring);
        if (!elem_equal(C, A, ring))
        {
            printf("FAIL: Q * B + R = A\n");
            elem_print(A, ring); printf("\n\n");
            elem_print(B, ring); printf("\n\n");
            elem_print(C, ring); printf("\n\n");
            elem_print(Q, ring); printf("\n\n");
            elem_print(R, ring); printf("\n\n");
            abort();
        }

        elem_randtest(A, state, size, ring);
        elem_randtest_not_zero(B, state, size, ring);
        elem_poly_divrem_basecase(Q, R, A, B, ring);
        elem_poly_divrem_basecase(A, R2, A, B, ring);
        if (!elem_equal(A, Q, ring) || !elem_equal(R, R2, ring))
        {
            printf("FAIL: aliasing Q, A\n");
            elem_print(A, ring); printf("\n\n");
            elem_print(B, ring); printf("\n\n");
            elem_print(Q, ring); printf("\n\n");
            elem_print(R, ring); printf("\n\n");
            elem_print(R2, ring); printf("\n\n");
            abort();
        }

        elem_randtest(A, state, size, ring);
        elem_randtest_not_zero(B, state, size, ring);
        elem_poly_divrem_basecase(Q, R, A, B, ring);
        elem_poly_divrem_basecase(Q2, A, A, B, ring);
        if (!elem_equal(A, R, ring) || !elem_equal(Q, Q2, ring))
        {
            printf("FAIL: aliasing R, A\n");
            elem_print(A, ring); printf("\n\n");
            elem_print(B, ring); printf("\n\n");
            elem_print(Q, ring); printf("\n\n");
            elem_print(Q2, ring); printf("\n\n");
            elem_print(R, ring); printf("\n\n");
            abort();
        }

        elem_randtest(A, state, size, ring);
        elem_randtest_not_zero(B, state, size, ring);
        elem_poly_divrem_basecase(Q, R, A, B, ring);
        elem_poly_divrem_basecase(Q2, B, A, B, ring);
        if (!elem_equal(B, R, ring) || !elem_equal(Q, Q2, ring))
        {
            printf("FAIL: aliasing R, B\n");
            elem_print(A, ring); printf("\n\n");
            elem_print(B, ring); printf("\n\n");
            elem_print(Q, ring); printf("\n\n");
            elem_print(Q2, ring); printf("\n\n");
            elem_print(R, ring); printf("\n\n");
            abort();
        }

        elem_randtest(A, state, size, ring);
        elem_randtest_not_zero(B, state, size, ring);
        elem_poly_divrem_basecase(Q, R, A, B, ring);
        elem_poly_divrem_basecase(B, R2, A, B, ring);
        if (!elem_equal(B, Q, ring) || !elem_equal(R, R2, ring))
        {
            printf("FAIL: aliasing Q, B\n");
            elem_print(A, ring); printf("\n\n");
            elem_print(B, ring); printf("\n\n");
            elem_print(Q, ring); printf("\n\n");
            elem_print(R, ring); printf("\n\n");
            elem_print(R2, ring); printf("\n\n");
            abort();
        }

        elem_clear(A, ring);
        elem_clear(B, ring);
        elem_clear(C, ring);
        elem_clear(Q, ring);
        elem_clear(Q2, ring);
        elem_clear(R, ring);
        elem_clear(R2, ring);

        ring_clear(ZZpx[2]);
        ring_clear(ZZpx[1]);
        ring_clear(ZZpx[0]);
        ring_clear(ZZp);
        ELEM_TMP_CLEAR(p, ZZ);
        ring_clear(ZZ);
    }

    printf("PASS\n");
    flint_randclear(state);
    return EXIT_SUCCESS;
}
Esempio n. 18
0
void GSwifi::bufferClear() {
    ring_clear(_buf_cmd);
}