aboutsummaryrefslogtreecommitdiff
path: root/functions_reference
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-11-24 04:51:55 +0100
committerCharles <sircharlesaze@gmail.com>2019-11-24 04:51:55 +0100
commit997cb1c39c603512b7db760e9537c39ea3c68a38 (patch)
treeb09df18114389b331a8b07254c78f881afb222b5 /functions_reference
parent5a47d63887a0878243b17799b19c4a0f76d3756d (diff)
downloadlibasm_test-997cb1c39c603512b7db760e9537c39ea3c68a38.tar.gz
libasm_test-997cb1c39c603512b7db760e9537c39ea3c68a38.tar.bz2
libasm_test-997cb1c39c603512b7db760e9537c39ea3c68a38.zip
Added reference bonus functions to test against assembly one
Diffstat (limited to 'functions_reference')
-rw-r--r--functions_reference/ref_ft_atoi_base.c44
-rw-r--r--functions_reference/ref_ft_list_push_front.c11
-rw-r--r--functions_reference/ref_ft_list_remove_if.c21
-rw-r--r--functions_reference/ref_ft_list_size.c17
-rw-r--r--functions_reference/ref_ft_list_sort.c45
5 files changed, 138 insertions, 0 deletions
diff --git a/functions_reference/ref_ft_atoi_base.c b/functions_reference/ref_ft_atoi_base.c
new file mode 100644
index 0000000..b2a4231
--- /dev/null
+++ b/functions_reference/ref_ft_atoi_base.c
@@ -0,0 +1,44 @@
+#include "libasm_test.h"
+
+static bool
+valid_base(char *base)
+{
+ if (strlen(base) < 2)
+ return false;
+ while (*base)
+ {
+ if (isspace(*base) || *base == '+' || *base == '-')
+ return false;
+ for (int i = 1; base[i]; i++)
+ if (base[i] == *base)
+ return false;
+ base++;
+ }
+ return true;
+}
+
+int
+ref_ft_atoi_base(char *str, char *base)
+{
+ long int nb;
+ int radix;
+ bool is_negative;
+
+ if (!valid_base(base))
+ return 0;
+ while (isspace(*str))
+ str++;
+ is_negative = false;
+ while (*str == '+' || *str == '-')
+ if (*str++ == '-')
+ is_negative = !is_negative;
+ radix = strlen(base);
+ nb = 0;
+ while (*str && strchr(base, *str) != NULL)
+ {
+ nb *= radix;
+ nb += strchr(base, *str) - base;
+ str++;
+ }
+ return is_negative ? -nb : nb;
+}
diff --git a/functions_reference/ref_ft_list_push_front.c b/functions_reference/ref_ft_list_push_front.c
new file mode 100644
index 0000000..821247d
--- /dev/null
+++ b/functions_reference/ref_ft_list_push_front.c
@@ -0,0 +1,11 @@
+#include "libasm_test.h"
+
+void
+ref_ft_list_push_front(t_list **begin_list, void *data)
+{
+
+ if (begin_list == NULL || data == NULL)
+ return ;
+ data->next = *begin_list;
+ *begin_list = data;
+}
diff --git a/functions_reference/ref_ft_list_remove_if.c b/functions_reference/ref_ft_list_remove_if.c
new file mode 100644
index 0000000..8b55da1
--- /dev/null
+++ b/functions_reference/ref_ft_list_remove_if.c
@@ -0,0 +1,21 @@
+#include "libasm_test.h"
+
+void
+ref_ft_list_remove_if(t_list **begin_list, void *data_ref,
+ int (*cmp)(), void (*free_fct)(void *))
+{
+ t_list *saved_next;
+
+ if (begin_list == NULL || *begin_list == NULL)
+ return ;
+ if (cmp(&(*begin_list)->val, data_ref) != 0)
+ {
+ ref_ft_list_remove_if(&(*begin_list)->next, data_ref, cmp, free_fct);
+ return ;
+ }
+ saved_next = (*begin_list)->next;
+ free_fct((*begin_list)->val);
+ free(*begin_list);
+ *begin_list = saved_next;
+ ref_ft_list_remove_if(begin_list, data_ref, cmp, free_fct);
+}
diff --git a/functions_reference/ref_ft_list_size.c b/functions_reference/ref_ft_list_size.c
new file mode 100644
index 0000000..7473bad
--- /dev/null
+++ b/functions_reference/ref_ft_list_size.c
@@ -0,0 +1,17 @@
+#include "libasm_test.h"
+
+int
+ref_ft_list_size(t_list *begin_list)
+{
+ int counter;
+
+ counter = 0;
+ while (begin_list)
+ {
+ counter++
+ begin_list = begin_list->next;
+ }
+ return counter;
+}
+
+
diff --git a/functions_reference/ref_ft_list_sort.c b/functions_reference/ref_ft_list_sort.c
new file mode 100644
index 0000000..b03c84b
--- /dev/null
+++ b/functions_reference/ref_ft_list_sort.c
@@ -0,0 +1,45 @@
+#include "libasm_test.h"
+
+static t_list*
+merge_sorted_list(t_list* l1, t_list* l2, int (*cmp)())
+{
+ t_list *merged = NULL;
+
+ if (l1 == NULL && l2 == NULL)
+ return NULL;
+ if (l1 == NULL)
+ return l2;
+ if (l2 == NULL)
+ return l1;
+ merged = cmp(l1->data, l2->data) < 0 ? l1 : l2;
+ if (cmp(l1->data, l2->data) < 0)
+ merged->next = merge_sorted_list(l1->next, l2, cmp);
+ else
+ merged->next = merge_sorted_list(l1, l2->next, cmp);
+ return merged;
+}
+
+void
+ref_ft_list_sort(t_list **begin_list, int (*cmp)())
+{
+ if (begin_list == NULL || *begin_list == NULL
+ || (*begin_list)->next == NULL)
+ return ;
+ t_list *fast = (*begin_list)->next;
+ t_list *slow = *begin_list;
+ while (fast != NULL)
+ {
+ fast = fast->next;
+ if (fast != NULL)
+ {
+ fast = fast->next;
+ slow = slow->next;
+ }
+ }
+ t_list *middle = slow->next;
+ slow->next = NULL;
+
+ sortList(begin_list, cmp);
+ sortList(&middle, cmp);
+ *begin_list = merge_sorted_list(*begin_list, middle, cmp);
+}