1
0
mirror of https://github.com/TREX-CoE/qmckl.git synced 2024-11-19 12:32:40 +01:00
qmckl/src/qmckl_context.org

411 lines
11 KiB
Org Mode
Raw Normal View History

2020-10-14 00:52:50 +02:00
# -*- mode: org -*-
2020-10-16 13:58:05 +02:00
# vim: syntax=c
2020-10-15 08:57:01 +02:00
#+TITLE: Context
2020-10-14 01:43:13 +02:00
This file is written in C because it is more natural to express the context in
C than in Fortran.
2020-10-14 01:43:13 +02:00
2020-10-16 19:42:12 +02:00
3 files are produced:
- a header file : =qmckl_context.h=
- a source file : =qmckl_context.c=
- a test file : =test_qmckl_context.c=
*** Header
#+BEGIN_SRC C :tangle qmckl_context.h
2020-10-16 13:58:05 +02:00
#ifndef QMCKL_CONTEXT_H
#define QMCKL_CONTEXT_H
#include "qmckl.h"
2020-10-16 19:42:12 +02:00
#+END_SRC
2020-10-14 01:43:13 +02:00
2020-10-16 19:42:12 +02:00
*** Source
#+BEGIN_SRC C :tangle qmckl_context.c
2020-10-14 00:52:50 +02:00
#include <stdlib.h> /* malloc */
#include "qmckl_context.h"
2020-10-16 19:42:12 +02:00
#+END_SRC
*** Test
#+BEGIN_SRC C :tangle test_qmckl_context.c
#include "qmckl_context.h"
2020-10-16 19:52:11 +02:00
#include <stdio.h>
2020-10-16 19:42:12 +02:00
int main() {
qmckl_exit_code rc; /* return code */
rc = QMCKL_SUCCESS;
#+END_SRC
2020-10-14 00:52:50 +02:00
* Context
2020-10-14 01:43:13 +02:00
2020-10-14 00:52:50 +02:00
The context variable is a handle for the state of the library, and
is stored in the following data structure, which can't be seen
2020-10-16 19:42:12 +02:00
outside of the library. To simplify compatibility with other
languages, the pointer to the internal data structure is converted
into a 64-bit signed integer, defined in the =qmckl_context= type.
A value of 0 for the context is equivalent to a NULL pointer.
2020-10-14 00:52:50 +02:00
2020-10-16 19:42:12 +02:00
*** Header
#+BEGIN_SRC C :tangle qmckl_context.h
/* 64-bit integer */
2020-10-14 01:43:13 +02:00
typedef long long int qmckl_context ;
2020-10-16 19:42:12 +02:00
#+END_SRC
2020-10-14 00:52:50 +02:00
2020-10-16 19:42:12 +02:00
*** Source
#+BEGIN_SRC C :tangle qmckl_context.c
2020-10-16 13:58:05 +02:00
typedef struct qmckl_context_struct {
struct qmckl_context_struct * prev;
2020-10-14 01:43:13 +02:00
int precision;
int range;
2020-10-14 00:52:50 +02:00
} qmckl_context_struct;
2020-10-16 19:42:12 +02:00
#+END_SRC
*** Test
We declare here the variables used in the tests.
#+BEGIN_SRC C :tangle test_qmckl_context.c
qmckl_context context;
qmckl_context new_context;
2020-10-16 19:52:11 +02:00
/* This needs to be repeated in the tests because we don't want to
expose it in the headers.
*/
typedef struct qmckl_context_struct {
struct qmckl_context_struct * prev;
int precision;
int range;
} qmckl_context_struct;
2020-10-16 19:42:12 +02:00
#+END_SRC
2020-10-14 00:52:50 +02:00
** =qmckl_context_create=
2020-10-14 00:52:50 +02:00
2020-10-16 19:42:12 +02:00
To create a new context, use =qmckl_context_create()=.
- On success, returns a pointer to a context using the =qmckl_context= type
- Returns 0 upon failure to allocate the internal data structure
2020-10-16 19:42:12 +02:00
*** Header
#+BEGIN_SRC C :tangle qmckl_context.h
2020-10-14 00:52:50 +02:00
qmckl_context qmckl_context_create();
2020-10-16 19:42:12 +02:00
#+END_SRC
2020-10-14 00:52:50 +02:00
2020-10-16 19:42:12 +02:00
*** Source
#+BEGIN_SRC C :tangle qmckl_context.c
2020-10-14 00:52:50 +02:00
qmckl_context qmckl_context_create() {
qmckl_context_struct* context;
context = (qmckl_context_struct*) malloc (sizeof(qmckl_context_struct));
if (context == NULL) {
return (qmckl_context) 0;
}
context->prev = NULL;
context->precision = QMCKL_DEFAULT_PRECISION;
context->range = QMCKL_DEFAULT_RANGE;
return (qmckl_context) context;
}
2020-10-16 19:42:12 +02:00
#+END_SRC
*** Test
#+BEGIN_SRC C :tangle test_qmckl_context.c
context = qmckl_context_create();
if (context == (qmckl_context) 0) {
2020-10-16 19:52:11 +02:00
fprintf(stderr,"qmckl_context_create\n");
2020-10-16 19:42:12 +02:00
rc = QMCKL_FAILURE;
}
if ( ((qmckl_context_struct*) new_context)->precision != QMCKL_DEFAULT_PRECISION ) {
2020-10-16 19:52:11 +02:00
fprintf(stderr,"qmckl_context_copy: No access to data\n");
2020-10-16 19:42:12 +02:00
rc = QMCKL_FAILURE;
}
#+END_SRC
2020-10-14 01:43:13 +02:00
** =qmckl_context_copy=
2020-10-14 00:52:50 +02:00
2020-10-16 19:42:12 +02:00
This function makes a shallow copy of the current context.
- Copying the 0-valued context returns 0
- On success, returns a pointer to the new context using the =qmckl_context= type
- Returns 0 upon failure to allocate the internal data structure
for the new context
*** Header
#+BEGIN_SRC C :tangle qmckl_context.h
qmckl_context qmckl_context_copy(const qmckl_context context);
#+END_SRC
2020-10-14 00:52:50 +02:00
2020-10-16 19:42:12 +02:00
*** Source
#+BEGIN_SRC C :tangle qmckl_context.c
qmckl_context qmckl_context_copy(const qmckl_context context) {
2020-10-14 00:52:50 +02:00
qmckl_context_struct* old_context;
qmckl_context_struct* new_context;
2020-10-16 19:42:12 +02:00
if (context == (qmckl_context) 0) {
return (qmckl_context) 0;
}
2020-10-14 00:52:50 +02:00
new_context = (qmckl_context_struct*) malloc (sizeof(qmckl_context_struct));
if (new_context == NULL) {
return (qmckl_context) 0;
}
old_context = (qmckl_context_struct*) context;
new_context->prev = old_context;
2020-10-14 01:43:13 +02:00
new_context->precision = old_context->precision;
2020-10-14 00:52:50 +02:00
new_context->range = old_context->range;
return (qmckl_context) new_context;
}
#+END_SRC
2020-10-14 01:43:13 +02:00
2020-10-16 19:42:12 +02:00
*** Test
#+BEGIN_SRC C :tangle test_qmckl_context.c
new_context = qmckl_context_copy(context);
if (new_context == (qmckl_context) 0) {
2020-10-16 19:52:11 +02:00
fprintf(stderr,"qmckl_context_copy: Allocation failure\n");
2020-10-16 19:42:12 +02:00
rc = QMCKL_FAILURE;
}
if (new_context == context ) {
2020-10-16 19:52:11 +02:00
fprintf(stderr,"qmckl_context_copy: Same pointer\n");
2020-10-16 19:42:12 +02:00
rc = QMCKL_FAILURE;
}
if ( ((qmckl_context_struct*) new_context)->precision != QMCKL_DEFAULT_PRECISION ) {
2020-10-16 19:52:11 +02:00
fprintf(stderr,"qmckl_context_copy: No access to data\n");
2020-10-16 19:42:12 +02:00
rc = QMCKL_FAILURE;
}
#+END_SRC
2020-10-16 19:42:12 +02:00
** =qmckl_context_previous=
Returns the previous context
- On success, returns the ancestor of the current context
- Returns 0 for the initial context
- Returns 0 for the 0-valued context
2020-10-16 19:42:12 +02:00
*** Header
#+BEGIN_SRC C :tangle qmckl_context.h
2020-10-16 19:42:12 +02:00
qmckl_context qmckl_context_previous(const qmckl_context context);
#+END_SRC
2020-10-16 19:42:12 +02:00
*** Source
#+BEGIN_SRC C :tangle qmckl_context.c
2020-10-16 19:42:12 +02:00
qmckl_context qmckl_context_previous(const qmckl_context context) {
2020-10-15 12:36:52 +02:00
qmckl_context_struct* ctx;
2020-10-16 19:42:12 +02:00
if (context == (qmckl_context) 0) {
return (qmckl_context) 0;
}
2020-10-15 12:36:52 +02:00
ctx = (qmckl_context_struct*) context;
2020-10-16 19:42:12 +02:00
return (qmckl_context) ctx->prev;
}
#+END_SRC
*** Test
#+BEGIN_SRC C :tangle test_qmckl_context.c
if (qmckl_context_previous(new_context) == (qmckl_context) 0) {
2020-10-16 19:52:11 +02:00
fprintf(stderr,"qmckl_context_copy: Null pointer\n");
2020-10-16 19:42:12 +02:00
rc = QMCKL_FAILURE;
}
if (qmckl_context_previous(new_context) != context) {
2020-10-16 19:52:11 +02:00
fprintf(stderr,"qmckl_context_copy: Wrong pointer\n");
2020-10-16 19:42:12 +02:00
rc = QMCKL_FAILURE;
}
if (qmckl_context_previous(context) != (qmckl_context) 0) {
2020-10-16 19:52:11 +02:00
fprintf(stderr,"qmckl_context_copy: Expected null pointer (1)\n");
2020-10-16 19:42:12 +02:00
rc = QMCKL_FAILURE;
}
if (qmckl_context_previous((qmckl_context) 0) != (qmckl_context) 0) {
2020-10-16 19:52:11 +02:00
fprintf(stderr,"qmckl_context_copy: Expected null pointer (2)\n");
2020-10-16 19:42:12 +02:00
rc = QMCKL_FAILURE;
}
#+END_SRC
** =qmckl_context_destroy=
Destroys the current context, leaving the ancestors untouched.
- Succeeds if the current context is properly destroyed
- Fails otherwise
- Fails is the 0-valued context is given in argument
The context given in parameter is overwritten by the 0-valued
context, so a pointer is passed to the function.
*** Header
#+BEGIN_SRC C :tangle qmckl_context.h
qmckl_exit_code qmckl_context_destroy(qmckl_context * context);
#+END_SRC
*** Source
#+BEGIN_SRC C :tangle qmckl_context.c
qmckl_exit_code qmckl_context_destroy(qmckl_context *context) {
qmckl_context_struct* ctx;
ctx = (qmckl_context_struct*) *context;
2020-10-15 12:36:52 +02:00
if (ctx == NULL) {
2020-10-16 19:42:12 +02:00
return QMCKL_FAILURE;
}
2020-10-15 12:36:52 +02:00
free(ctx);
2020-10-16 19:42:12 +02:00
*context = (qmckl_context) 0;
return QMCKL_SUCCESS;
}
#+END_SRC
2020-10-14 00:52:50 +02:00
2020-10-16 19:42:12 +02:00
*** Test
#+BEGIN_SRC C :tangle test_qmckl_context.c
if (new_context == (qmckl_context) 0) {
2020-10-16 19:52:11 +02:00
fprintf(stderr,"qmckl_context_destroy: new_context is NULL\n");
2020-10-16 19:42:12 +02:00
rc = QMCKL_FAILURE;
}
if (qmckl_context_destroy(&new_context) == QMCKL_FAILURE) {
2020-10-16 19:52:11 +02:00
fprintf(stderr,"qmckl_context_destroy: Unable to destroy the new_context\n");
2020-10-16 19:42:12 +02:00
rc = QMCKL_FAILURE;
}
if (new_context != (qmckl_context) 0) {
2020-10-16 19:52:11 +02:00
fprintf(stderr,"qmckl_context_destroy: new_context should be NULL\n");
2020-10-16 19:42:12 +02:00
rc = QMCKL_FAILURE;
}
if (qmckl_context_destroy((qmckl_context) 0) == QMCKL_SUCCESS) {
2020-10-16 19:52:11 +02:00
fprintf(stderr,"qmckl_context_destroy: Failure expected with NULL pointer\n");
2020-10-16 19:42:12 +02:00
rc = QMCKL_FAILURE;
}
#+END_SRC
2020-10-14 00:52:50 +02:00
* Precision
The following functions set and get the expected required precision
and range. =precision= should be an integer between 2 and 53, and
=range= should be an integer between 2 and 11.
2020-10-16 13:58:05 +02:00
The setter functions functions return a new context as a 64-bit integer.
The getter functions return the value, as a 32-bit integer.
2020-10-16 13:58:05 +02:00
The update functions return =QMCKL_SUCCESS= or =QMCKL_FAILURE=.
2020-10-14 00:52:50 +02:00
2020-10-16 13:58:05 +02:00
** =qmckl_context_update_precision=
#+BEGIN_SRC C :tangle qmckl_context.h
2020-10-16 13:58:05 +02:00
qmckl_exit_code qmckl_context_update_precision(const qmckl_context context, int precision);
#+END_SRC
2020-10-14 00:52:50 +02:00
#+BEGIN_SRC C :tangle qmckl_context.c
2020-10-16 13:58:05 +02:00
qmckl_exit_code qmckl_context_update_precision(const qmckl_context context, int precision) {
2020-10-14 00:52:50 +02:00
qmckl_context_struct* ctx;
2020-10-16 13:58:05 +02:00
if (precision < 2) return QMCKL_FAILURE;
if (precision > 53) return QMCKL_FAILURE;
ctx = (qmckl_context_struct*) context;
if (ctx == NULL) return QMCKL_FAILURE;
2020-10-14 00:52:50 +02:00
ctx->precision = precision;
2020-10-16 13:58:05 +02:00
return QMCKL_SUCCESS;
}
#+END_SRC
** =qmckl_context_update_range=
#+BEGIN_SRC C :tangle qmckl_context.h
qmckl_exit_code qmckl_context_update_range(const qmckl_context context, int range);
#+END_SRC
#+BEGIN_SRC C :tangle qmckl_context.c
qmckl_exit_code qmckl_context_update_range(const qmckl_context context, int range) {
qmckl_context_struct* ctx;
if (range < 2) return QMCKL_FAILURE;
if (range > 11) return QMCKL_FAILURE;
ctx = (qmckl_context_struct*) context;
if (ctx == NULL) return QMCKL_FAILURE;
ctx->range = range;
return QMCKL_SUCCESS;
}
#+END_SRC
** =qmckl_context_set_precision=
#+BEGIN_SRC C :tangle qmckl_context.h
qmckl_context qmckl_context_set_precision(const qmckl_context context, int precision);
#+END_SRC
#+BEGIN_SRC C :tangle qmckl_context.c
qmckl_context qmckl_context_set_precision(const qmckl_context context, const int precision) {
qmckl_context new_context;
new_context = qmckl_context_copy(context);
if (new_context == 0) return 0;
if (qmckl_context_update_precision(context, precision) == QMCKL_FAILURE) return 0;
return new_context;
2020-10-14 00:52:50 +02:00
}
#+END_SRC
2020-10-14 00:52:50 +02:00
** =qmckl_context_set_range=
#+BEGIN_SRC C :tangle qmckl_context.h
qmckl_context qmckl_context_set_range(const qmckl_context context, int range);
#+END_SRC
2020-10-14 00:52:50 +02:00
#+BEGIN_SRC C :tangle qmckl_context.c
qmckl_context qmckl_context_set_range(const qmckl_context context, int range) {
2020-10-16 13:58:05 +02:00
qmckl_context new_context;
2020-10-14 00:52:50 +02:00
2020-10-16 13:58:05 +02:00
new_context = qmckl_context_copy(context);
if (new_context == 0) return 0;
2020-10-14 00:52:50 +02:00
2020-10-16 13:58:05 +02:00
if (qmckl_context_update_range(context, range) == QMCKL_FAILURE) return 0;
return new_context;
2020-10-14 00:52:50 +02:00
}
#+END_SRC
2020-10-14 00:52:50 +02:00
** =qmckl_context_get_precision=
2020-10-14 00:52:50 +02:00
#+BEGIN_SRC C :tangle qmckl_context.h
int qmckl_context_get_precision(const qmckl_context context);
#+END_SRC
#+BEGIN_SRC C :tangle qmckl_context.c
int qmckl_context_get_precision(const qmckl_context context) {
qmckl_context_struct* ctx;
ctx = (qmckl_context_struct*) context;
return ctx->precision;
}
#+END_SRC
** =qmckl_context_get_range=
#+BEGIN_SRC C :tangle qmckl_context.h
int qmckl_context_get_range(const qmckl_context context);
#+END_SRC
#+BEGIN_SRC C :tangle qmckl_context.c
int qmckl_context_get_range(const qmckl_context context) {
qmckl_context_struct* ctx;
ctx = (qmckl_context_struct*) context;
return ctx->range;
}
#+END_SRC
2020-10-14 00:52:50 +02:00
2020-10-16 13:58:05 +02:00
2020-10-16 19:42:12 +02:00
* End of files
2020-10-16 13:58:05 +02:00
2020-10-16 19:42:12 +02:00
*** Header
#+BEGIN_SRC C :tangle qmckl_context.h
2020-10-16 13:58:05 +02:00
#endif
2020-10-16 19:42:12 +02:00
#+END_SRC
*** Test
#+BEGIN_SRC C :tangle test_qmckl_context.c
return QMCKL_SUCCESS;
}
#+END_SRC
2020-10-16 13:58:05 +02:00