David Blume's GitList
Repositories
testcode.git
Code
Commits
Branches
Tags
Search
Tree:
c7de061
Branches
Tags
c++11
main
start
testcode.git
product
main.cpp
Added a histogram thing.
dblume
commited
c7de061
at 2023-10-18 23:38:10
main.cpp
Blame
History
Raw
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <memory> #include <complex> #include <cassert> #include "main_helper.hpp" #include "scoped_set_adder.hpp" #include "ui/widget.hpp" #include "../include/my_lib.hpp" #include "thread_with_lambda.hpp" #include "thread_manager.hpp" #include "deadlock/deadlock.hpp" #include "next_power_of_two.hpp" #include "unistd.h" #include "mmmm.hpp" using namespace std; using namespace MY_LIB; int length_of_array; ostream& operator<<(ostream& ost, const int a[]) { // bad form: using global length_of_array for (int i = 0; i < length_of_array; ++i) { ost << a[i] << " "; } return ost; } // remove all instances of element n, return new length int remove_element(int a[], int n, int elem) { int new_length = 0; for (int i = 0; i < n; ++i) { if (a[i] != elem) { if (i != new_length) { a[new_length] = a[i]; } ++new_length; } } return new_length; } // remove all duplicates of an element, return new length int remove_duplicates(int a[], int n) { int last = 0; for (int i = 1; i < n; ++i) { if (a[last] != a[i]) { ++last; a[last] = a[i]; } } return last + 1; } void test_cpp14() { #if __cplusplus == 201402L // Store a generalized lambda, that squares a number, in a variable auto func = [](auto input) { return input * input; }; // square of an int std::cout << func(10) << std::endl; // square of a double std::cout << func(2.345) << std::endl; // square of a complex number std::cout << func(std::complex<double>(3, -2)) << std::endl; #endif } struct Base { Base(): x_(0) {} virtual ~Base() {} virtual void f() {} int x_; }; struct Derived : Base { virtual void name() {} public: int g() { return 8; } }; template <typename T> void writeNumber(int fd, T val, int base) { if (val == 0) { ssize_t bytes = write(fd, "0", 1); (void)bytes; return; } const int buflen = sizeof(val) * 8; static char buf[buflen] = {0}; printf("* buf starts at %p and ends at %p and is %ld long\n", buf, &buf[(sizeof(val)*8)-1], sizeof(val)*8); int i = 30; for(; val && i ; --i, val /= base) { //for(; val && i && (static_cast<int>(sizeof(val))*8 >= i); --i, val /= base) printf("** i=%d, writing %c to %p\n", i, "0123456789abcdef"[val % base], &buf[i-1]); buf[i-1] = "0123456789abcdef"[val % base]; } printf("** i=%d writing %d bytes %p\n", i, buflen-i, &buf[i+1]); ssize_t bytes = write(fd, &buf[i], buflen - i); (void)bytes; } int main() { unsigned long long v2 = 0xF102030405060708; writeNumber(1, v2, 16); cout << endl; writeNumber(1, v2, 10); cout << endl; writeNumber(1, v2, 8); writeNumber(1, v2, 2); cout << endl << "DXB end!" << endl; length_of_array = 6; int a[6] = {10, 20, 20, 30, 40, 40}; int item_to_remove = 20; // thread_with_lambda(); Base* pb = new Derived; std::shared_ptr<Base> sp(pb); cout << "pb is " << pb << endl; cout << "sp is " << sp << endl; Derived* pd = dynamic_cast<Derived*>(sp.get()); if (pd) { cout << "pd exists " << endl; } else { cout << "pd is NULL " << endl; } cout << "pd is " << pd << endl; cout << "Removing " << item_to_remove << endl; length_of_array = remove_element(a, length_of_array, item_to_remove); cout << "a became " << a << endl; length_of_array = 6; int b[6] = {10, 20, 20, 30, 40, 40}; length_of_array = remove_duplicates(b, length_of_array); cout << "Removing duplicates yielded " << length_of_array << " items " << b << endl; main_helper_fn(); UI::widget_cb(); cout << "sizeof(int) is " << sizeof(int) << "." << endl; my_lib_fn(); my_lib_fn(5); my_lib_rect r(16, 9); cout << "area of rect r is " << r.area() << "." << endl; test_cpp14(); { std::unique_ptr<int> up(new int()); scoped_set_adder set_adder("a set_adder object"); scoped_set_adder another_set_adder("and another set_adder"); cout << "unique_ptr " << (up ? "passes" : "fails") << " if." << endl; delete up.release(); cout << "unique_ptr after release " << (up ? "passes" : "fails") << " if." << endl; cout << "unique_ptr.get() after release " << (up.get() ? "passes" : "fails") << " if." << endl; scoped_set_adder::dump(); } { // Testing Herb Sutter's pImpl template #if __cplusplus == 201402L std::unique_ptr<MyClass> my_class(std::make_unique<MyClass>()); my_class->f(); #endif } // cout << "deadlock() returned " << deadlock() << endl; for (unsigned i : {0, 1, 2, 3, 4, 5, 6, 7, 31, 32, 33, 4003, 65535, 65536, 65537}) { cout << "next_power_of_two(" << i << ") = " << round_up_to_next_power_of_two(i) << endl; assert(round_up_to_next_power_of_two(i) == another_round_up_to_next_power_of_two(i)); } int some_long_variable_name{0}; if (some_long_variable_name + some_long_variable_name + some_long_variable_name + some_long_variable_name + some_long_variable_name) { cout << "yep" << endl; } //test_mmmm(); // uses stdin cout << "Done." << endl; return 0; }