<commit_before>/*
 * This file is part of RTRlib.
 *
 * RTRlib is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or (at your
 * option) any later version.
 *
 * RTRlib is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with RTRlib; see the file COPYING.LESSER.
 *
 * written by <NAME>, in cooperation with:
 * INET group, Hamburg University of Applied Sciences,
 * CST group, Freie Universitaet Berlin
 * Website: http://rpki.realmv6.org/
 */

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <assert.h>
#include <string.h>
#include <arpa/inet.h>
#include "rtrlib/lib/ipv6.h"
#include "rtrlib/lib/log.h"
#include "rtrlib/lib/utils.h"
#include "rtrlib/pfx/lpfst/lpfst.c"


void get_bits_testv4(){
    ip_addr addr;
    addr.ver = IPV4;

    ip_addr result;
    addr.u.addr4.addr=0xAABBCC22;

    result = ip_addr_get_bits(&addr, 0, 32);
    assert(result.u.addr4.addr == 0xAABBCC22);

    result = ip_addr_get_bits(&addr, 0, 1);
    assert(result.u.addr4.addr == 0x80000000);

    result = ip_addr_get_bits(&addr, 1, 1);
    assert(result.u.addr4.addr == 0);

    result = ip_addr_get_bits(&addr, 2, 1);
    assert(result.u.addr4.addr == 0x20000000);

    result = ip_addr_get_bits(&addr, 0, 8);
    assert(result.u.addr4.addr == 0xAA000000);

    result = ip_addr_get_bits(&addr, 8, 8);
    assert(result.u.addr4.addr == 0x00BB0000);


    rtr_str_to_ipaddr("10.10.10.0", &addr);

    result = ip_addr_get_bits(&addr, 0, 8);
    assert(rtr_cmp_ipv4(&result, "10.0.0.0"));

    result = ip_addr_get_bits(&addr, 0, 16);
    assert(rtr_cmp_ipv4(&result, "10.10.0.0"));

    result = ip_addr_get_bits(&addr, 8, 8);
    assert(rtr_cmp_ipv4(&result, "0.10.0.0"));

    result = ip_addr_get_bits(&addr, 8, 24);
    assert(rtr_cmp_ipv4(&result, "0.10.10.0"));

    result = ip_addr_get_bits(&addr, 31, 1);
    assert(result.u.addr4.addr == 0);

    result = ip_addr_get_bits(&addr, 0, 1);
    assert(result.u.addr4.addr == 0);

    result = ip_addr_get_bits(&addr, 3, 3);
    assert(rtr_cmp_ipv4(&result, "8.0.0.0"));

    addr.u.addr4.addr = 0x6D698000;
    result = ip_addr_get_bits(&addr, 0, 19);
    assert(result.u.addr4.addr == 0x6D698000);
    /*
    rtr_str_to_ipaddr("172.16.58.3", &addr);
    result = ip_addr_get_bits(&addr, 0, 8);
    printf("%u\n", result.u.addr4.addr);
    */

    char buf[INET_ADDRSTRLEN];
    assert(rtr_str_to_ipaddr("10.10.10.5", &addr) == 0);
    assert(rtr_ipaddr_to_str(&addr, buf, sizeof(buf)) == 0);
    assert(strcmp("10.10.10.5", buf) == 0);

}

void get_bits_testv6(){
    ip_addr addr;
    addr.ver = IPV6;
    addr.u.addr6.addr[0] = 0x22AABBCC;
    addr.u.addr6.addr[1] = 0xDDEEFF99;
    addr.u.addr6.addr[2] = 0x33001122;
    addr.u.addr6.addr[3] = 0x33445566;

    ip_addr result;

    result = ip_addr_get_bits(&addr, 0, 128);
    assert(result.u.addr6.addr[0] == addr.u.addr6.addr[0] && result.u.addr6.addr[1] == addr.u.addr6.addr[1] && result.u.addr6.addr[2] == addr.u.addr6.addr[2] && result.u.addr6.addr[3] == addr.u.addr6.addr[3]);

    result = ip_addr_get_bits(&addr, 0, 64);
    assert(result.u.addr6.addr[0] == addr.u.addr6.addr[0] && result.u.addr6.addr[1] == addr.u.addr6.addr[1] && result.u.addr6.addr[2] == 0 && result.u.addr6.addr[3] == 0);

    bzero(&result, sizeof(result));
    result = ip_addr_get_bits(&addr, 64, 64);
    assert(result.u.addr6.addr[0] == 0);
    assert(result.u.addr6.addr[1] == 0);
    assert(result.u.addr6.addr[2] == addr.u.addr6.addr[2]);
    assert(result.u.addr6.addr[3] == addr.u.addr6.addr[3]);


    result = ip_addr_get_bits(&addr, 0, 8);
    assert(result.u.addr6.addr[0] == 0x22000000 && result.u.addr6.addr[1] == 0);<commit_msg>tests/test_lpfst.c: testcase added<commit_after>/*
 * This file is part of RTRlib.
 *
 * RTRlib is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or (at your
 * option) any later version.
 *
 * RTRlib is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with RTRlib; see the file COPYING.LESSER.
 *
 * written by <NAME>, in cooperation with:
 * INET group, Hamburg University of Applied Sciences,
 * CST group, Freie Universitaet Berlin
 * Website: http://rpki.realmv6.org/
 */

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <assert.h>
#include <string.h>
#include <arpa/inet.h>
#include "rtrlib/lib/ipv6.h"
#include "rtrlib/lib/log.h"
#include "rtrlib/lib/utils.h"
#include "rtrlib/pfx/lpfst/lpfst.c"


void get_bits_testv4(){
    ip_addr addr;
    addr.ver = IPV4;

    ip_addr result;
    addr.u.addr4.addr=0xAABBCC22;

    result = ip_addr_get_bits(&addr, 0, 32);
    assert(result.u.addr4.addr == 0xAABBCC22);

    result = ip_addr_get_bits(&addr, 0, 1);
    assert(result.u.addr4.addr == 0x80000000);

    result = ip_addr_get_bits(&addr, 1, 1);
    assert(result.u.addr4.addr == 0);

    result = ip_addr_get_bits(&addr, 2, 1);
    assert(result.u.addr4.addr == 0x20000000);

    result = ip_addr_get_bits(&addr, 0, 8);
    assert(result.u.addr4.addr == 0xAA000000);

    result = ip_addr_get_bits(&addr, 8, 8);
    assert(result.u.addr4.addr == 0x00BB0000);


    rtr_str_to_ipaddr("10.10.10.0", &addr);

    result = ip_addr_get_bits(&addr, 0, 8);
    assert(rtr_cmp_ipv4(&result, "10.0.0.0"));

    result = ip_addr_get_bits(&addr, 0, 16);
    assert(rtr_cmp_ipv4(&result, "10.10.0.0"));

    result = ip_addr_get_bits(&addr, 8, 8);
    assert(rtr_cmp_ipv4(&result, "0.10.0.0"));

    result = ip_addr_get_bits(&addr, 8, 24);
    assert(rtr_cmp_ipv4(&result, "0.10.10.0"));

    result = ip_addr_get_bits(&addr, 31, 1);
    assert(result.u.addr4.addr == 0);

    result = ip_addr_get_bits(&addr, 0, 1);
    assert(result.u.addr4.addr == 0);

    result = ip_addr_get_bits(&addr, 3, 3);
    assert(rtr_cmp_ipv4(&result, "8.0.0.0"));

    addr.u.addr4.addr = 0x6D698000;
    result = ip_addr_get_bits(&addr, 0, 19);
    assert(result.u.addr4.addr == 0x6D698000);
    /*
    rtr_str_to_ipaddr("172.16.58.3", &addr);
    result = ip_addr_get_bits(&addr, 0, 8);
    printf("%u\n", result.u.addr4.addr);
    */

    char buf[INET_ADDRSTRLEN];
    assert(rtr_str_to_ipaddr("10.10.10.5", &addr) == 0);
    assert(rtr_ipaddr_to_str(&addr, buf, sizeof(buf)) == 0);
    assert(strcmp("10.10.10.5", buf) == 0);

    assert(rtr_str_to_ipaddr("172.16.31.10", &addr) == 0);
    result = ip_addr_get_bits(&addr, 0, 1);
    assert(result.u.addr4.addr == 0x80000000);

    assert(rtr_str_to_ipaddr("192.168.127.12", &addr) == 0);
    result = ip_addr_get_bits(&addr, 0, 1);
    printf("%x\n", result.u.addr4.addr);
    assert(result.u.addr4.addr == 0x80000000);
}

void get_bits_testv6(){
    ip_addr addr;
    addr.ver = IPV6;
    addr.u.addr6.addr[0] = 0x22AABBCC;
    addr.u.addr6.addr[1] = 0xDDEEFF99;
    addr.u.addr6.addr[2] = 0x33001122;
    addr.u.addr6.addr[3] = 0x33445566;

    ip_addr result;

    result = ip_addr_get_bits(&addr, 0, 128);
    assert(result.u.addr6.addr[0] == addr.u.addr6.addr[0] && result.u.addr6.addr[1] == addr.u.addr6.addr[1] && result.u.addr6.addr[2] == addr.u.addr6.addr[2] && result.u.addr6.addr[3] == addr.u.addr6.addr[3]);

    result = ip_addr_get_bits(&addr, 0, 64);
    assert(result.u.addr6.addr[0] == addr.u.addr6.addr[0] && result.u.addr6.addr[1] == addr.u.addr6.addr[1] && result.u.addr6.addr[2] == 0 && result.u.addr6.addr[3] == 0);

    bzero(&result, sizeof(result));
    result = ip_addr_get_bits(&addr, 64, 64);
    assert(result.u.addr6.addr[0] == 0);
    assert(result.u.addr6.addr[1] == 0);
    assert(result.u.addr6.addr[2] == addr.u.addr6.addr[2]);
    assert(result.u.addr6.addr[3] == addr.u.addr6.addr[3]);


    result = ip_addr_get_bits(&addr, 0, 8);
    assert(result.u.addr6.addr[0] == 0x22000000 && result.u.addr6.addr[1] == 0);
{'fraction_non_alphanumeric': 0.16923620101212192, 'fraction_numerical': 0.07073084618100506, 'mean_word_length': 3.6034669555796315, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 20, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1312008', 'n_tokens_mistral': 3761, 'n_tokens_neox': 3406, 'n_words': 671}