<commit_before>/**
 * $Id: unittest-shm.c,v 1.2 2008/07/30 15:01:12 ylafon Exp $
 *
 * (c) 2008 by <NAME>
 *      See COPYING file for copying and redistribution conditions.
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; version 2 of the License.
 *
 *      This program 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 General Public License for more details.
 *
 * Contact: <<EMAIL>>
 */

#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/sem.h>

#include "defs.h"
#include "types.h"
#include "ortho.h"
#include "lines.h"
#include "winds.h"
#include "grib.h"
#include "context.h"
#include "polar.h"
#include "waypoint.h"
#include "gshhs.h"
#include "shmem.h"

vlmc_context *global_vlmc_context;

int main (int argc, char **argv) {
  double lat1,long1, lat2, long2, lat3, long3;
  double lata, longa, latb, longb;
  
  double lat_boat, long_boat;
  time_t current_time, previ_time, crossing_time;
  int i;
  wind_info wind_boat;
  waypoint fake_waypoint;
  int shmid, semid;
  void *segmaddr;
  struct sembuf sem_op[2];

  global_vlmc_context = calloc(1, sizeof(vlmc_context));
  init_context_default(global_vlmc_context);

  lat1 = degToRad(54.793253356367);
  long1 = degToRad(-163.35368070813);
  lat2 = degToRad(54.789622936359);
  long2 = degToRad(-163.36603822435);

  init_coastline();
  latb = check_coast(lat1, long1, lat2, long2, &lata, &longa);
  if (latb >= INTER_MIN_LIMIT && latb <= INTER_MAX_LIMIT) {
	printf("Coast crossed %.4f, %.4f\n", radToDeg(lata), radToDeg(longa));
  } else {
	printf("Coast avoided\n");
  }
  latb = intersects(lat1, long1, lat2, long2, degToRad(54.8089), degToRad(-163.381), degToRad(54.7696), degToRad(-163.348), &lata, &longa);
  if (latb >= INTER_MIN_LIMIT && latb <= INTER_MAX_LIMIT) {
        printf("Intersection %.4f, %.4f\n", radToDeg(lata), radToDeg(longa));
  } else {
        printf("No intersection\n");
  }

  lat1  = degToRad(10);
  long1 = degToRad(10);
  
  lat2  = degToRad(11.1);
  long2 = degToRad(9);

  lat3  = degToRad(30);
  long3 = degToRad(-10);

  lata  = degToRad(10);
  longa = degToRad(9);

  latb  = degToRad(11.1);
  longb = degToRad(10);

  printf("Unit test: distance from Point 1 to Line A-B\n");
  printf("Point1: lat %.3f, long %.3f\n", radToDeg(lat1), radToDeg(long1));
  printf("Line A: lat %.3f, long %.3f\n", radToDeg(lata), radToDeg(longa));
  printf("Line B: lat %.3f, long %.3f\n", radToDeg(latb), radToDeg(longb));

  printf("Distance Point1 -> Point A: %.3f\n", ortho_distance(lat1, long1,
							      lata, longa));
  printf("Distance Point1 -> Point B: %.3f\n", ortho_distance(lat1, long1,
							      latb, longb));
  printf("Distance Point1 -> Line A-B: %.3f\n", 
	 distance_to_line(lat1, long1, lata, longa, latb, longb));

  printf("\nUnit test: distance from Point 2 to Line A-B\n");
  printf("Point2: lat %.3f, long %.3f\n", radToDeg(lat2), radToDeg(long2));
  printf("Line A: lat %.3f, long %.3f\n", radToDeg(lata), radToDeg(longa));
  printf("Line B: lat %.3f, long %.3f\n", radToDeg(latb), radToDeg(longb));

  printf("Distance Point2 -> Point A: %.3f\n", ortho_distance(lat2, long2,
							      lata, longa));
  printf("Distance Point2 -> Point B: %.3f\n", ortho_distance(lat2, long2,
							      latb, longb));
  printf("Distance Point2 -> Line A-B: %.3f\n", 
	 distance_to_line(lat2, long2, lata, longa, latb, longb));

  printf("\nUnit test: distance from Point 3 to Line A-B\n");
  printf("Point3: lat %.3f, long %.3f\n", radToDeg(lat3), radToDeg(long3));
  printf("Line A: lat %.3f, long %.3f\n", radToDeg(lata), radToDeg(longa));
  printf("Line B: lat %.3f, long %.3f\n", radToDeg(latb), radToDeg(longb));

  printf("Distance Point3 -> Point A: %.3f\n", ortho_distance(lat3, long3,
							      lata, longa));
  printf("Distance Point3 -> Point B: %.3f\n", ortho_distance(lat3, long3,
							      latb, longb));
  printf("Distance Point3 -> Line A-B: %.3f\n", 
	 distance_to_line(lat3, long3, lata, longa, latb, longb));

  init_polar();
  printf("\nWind test\n");
  
  semid = get_semaphore_id();
  if (semid == -1) {
    fprintf(stderr, "Unable to get the semaphore\n");
    exit(1);
  }
  sem_op[0].sem_num = 0;
  sem_op[0].sem_op  = 0;
  sem_op[0].sem_flg = SEM_UNDO;
  sem_op[1].sem_num = 0;
  sem_op[1].sem_op  = 1;
  sem_op[1].sem_flg = SEM_UNDO|IPC_NOWAIT;
  if (semop(semid, sem_op, 2) == -1) {
    fprintf(stderr, "Fail to lock the semaphore\n");
    exit(1);
  }

  shmid =  get_grib_shmid();
  if (shmid == -1) {
    /* not there, we create it */
    fprintf(stderr, "Cannot find GRIB shared segment\n");
    exit(1);
  }

  segmaddr = get_grib_shmem(shmid, 1);
  construct_grib_array_from_shmem(&global_vlmc_context->windtable, segmaddr);

  time(&current_time);
  lat_boat     = degToRad(39.812);
  long_boat    = degToRad(8.43);
  for (i=0; i<4; current_time += 15*60, i++) {
    printf("Date: %s", ctime(&current_time));
    /* each 15mn */
    get_wind_info_latlong_UV(lat_boat, long_boat, 
			     current_time,
			     &wind_boat);
    printf("UV   Wind at  lat: %.2f long: %.2f, speed %.1f angle %.1f\n",
	   radToDeg(lat_boat), radToDeg(long_boat),
	   wind_boat.speed, radToDeg(wind_boat.angle));
    get_wind_info_latlong_TWSA(lat_boat, long_boat, 
			       current_time,
			       &wind_boat);
    printf("TWSA Wind at  lat: %.2f long: %.2f, speed %.1f angle %.1f\n",
	   radToDeg(lat_boat), radToDeg(long_boat),
	   wind_boat.speed, radToDeg(wind_boat.angle));
  }

  shmdt(segmaddr);
  /* and release the semaphore */
  sem_op[0].sem_num = 0;
  sem_op[0].sem_op  = -1;
  sem_op[0].sem_flg = SEM_UNDO|IPC_NOWAIT;
  if (semop(semid, sem_op, 1) == -1) {
    fprintf(stderr, "Fail to unlock the semaphore\n");
    exit(1);
  }

  printf("\nWaypoint crossing:\n");<commit_msg>added two mode, (set at compilation time)<commit_after>/**
 * $Id: unittest-shm.c,v 1.3 2008/07/31 12:29:49 ylafon Exp $
 *
 * (c) 2008 by <NAME>
 *      See COPYING file for copying and redistribution conditions.
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; version 2 of the License.
 *
 *      This program 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 General Public License for more details.
 *
 * Contact: <<EMAIL>>
 */

#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/sem.h>

#include "defs.h"
#include "types.h"
#include "ortho.h"
#include "lines.h"
#include "winds.h"
#include "grib.h"
#include "context.h"
#include "polar.h"
#include "waypoint.h"
#include "gshhs.h"
#include "shmem.h"

vlmc_context *global_vlmc_context;

int main (int argc, char **argv) {
  double lat1,long1, lat2, long2, lat3, long3;
  double lata, longa, latb, longb;
  
  double lat_boat, long_boat;
  time_t current_time, crossing_time;
  int i;
  wind_info wind_boat;
  waypoint fake_waypoint;
  int shmid, semid;
  void *segmaddr;
  struct sembuf sem_op[2];

  global_vlmc_context = calloc(1, sizeof(vlmc_context));
  init_context_default(global_vlmc_context);

  lat1 = degToRad(54.793253356367);
  long1 = degToRad(-163.35368070813);
  lat2 = degToRad(54.789622936359);
  long2 = degToRad(-163.36603822435);

  init_coastline();
  latb = check_coast(lat1, long1, lat2, long2, &lata, &longa);
  if (latb >= INTER_MIN_LIMIT && latb <= INTER_MAX_LIMIT) {
	printf("Coast crossed %.4f, %.4f\n", radToDeg(lata), radToDeg(longa));
  } else {
	printf("Coast avoided\n");
  }
  latb = intersects(lat1, long1, lat2, long2, degToRad(54.8089), degToRad(-163.381), degToRad(54.7696), degToRad(-163.348), &lata, &longa);
  if (latb >= INTER_MIN_LIMIT && latb <= INTER_MAX_LIMIT) {
        printf("Intersection %.4f, %.4f\n", radToDeg(lata), radToDeg(longa));
  } else {
        printf("No intersection\n");
  }

  lat1  = degToRad(10);
  long1 = degToRad(10);
  
  lat2  = degToRad(11.1);
  long2 = degToRad(9);

  lat3  = degToRad(30);
  long3 = degToRad(-10);

  lata  = degToRad(10);
  longa = degToRad(9);

  latb  = degToRad(11.1);
  longb = degToRad(10);

  printf("Unit test: distance from Point 1 to Line A-B\n");
  printf("Point1: lat %.3f, long %.3f\n", radToDeg(lat1), radToDeg(long1));
  printf("Line A: lat %.3f, long %.3f\n", radToDeg(lata), radToDeg(longa));
  printf("Line B: lat %.3f, long %.3f\n", radToDeg(latb), radToDeg(longb));

  printf("Distance Point1 -> Point A: %.3f\n", ortho_distance(lat1, long1,
							      lata, longa));
  printf("Distance Point1 -> Point B: %.3f\n", ortho_distance(lat1, long1,
							      latb, longb));
  printf("Distance Point1 -> Line A-B: %.3f\n", 
	 distance_to_line(lat1, long1, lata, longa, latb, longb));

  printf("\nUnit test: distance from Point 2 to Line A-B\n");
  printf("Point2: lat %.3f, long %.3f\n", radToDeg(lat2), radToDeg(long2));
  printf("Line A: lat %.3f, long %.3f\n", radToDeg(lata), radToDeg(longa));
  printf("Line B: lat %.3f, long %.3f\n", radToDeg(latb), radToDeg(longb));

  printf("Distance Point2 -> Point A: %.3f\n", ortho_distance(lat2, long2,
							      lata, longa));
  printf("Distance Point2 -> Point B: %.3f\n", ortho_distance(lat2, long2,
							      latb, longb));
  printf("Distance Point2 -> Line A-B: %.3f\n", 
	 distance_to_line(lat2, long2, lata, longa, latb, longb));

  printf("\nUnit test: distance from Point 3 to Line A-B\n");
  printf("Point3: lat %.3f, long %.3f\n", radToDeg(lat3), radToDeg(long3));
  printf("Line A: lat %.3f, long %.3f\n", radToDeg(lata), radToDeg(longa));
  printf("Line B: lat %.3f, long %.3f\n", radToDeg(latb), radToDeg(longb));

  printf("Distance Point3 -> Point A: %.3f\n", ortho_distance(lat3, long3,
							      lata, longa));
  printf("Distance Point3 -> Point B: %.3f\n", ortho_distance(lat3, long3,
							      latb, longb));
  printf("Distance Point3 -> Line A-B: %.3f\n", 
	 distance_to_line(lat3, long3, lata, longa, latb, longb));

  init_polar();
  printf("\nWind test\n");
  
  semid = get_semaphore_id();
  if (semid == -1) {
    fprintf(stderr, "Unable to get the semaphore\n");
    exit(1);
  }
  sem_op[0].sem_num = 0;
  sem_op[0].sem_op  = 0;
#ifdef SAFE_SHM_READ
  sem_op[0].sem_flg = SEM_UNDO;
  sem_op[1].sem_num = 0;
  sem_op[1].sem_op  = 1;
  sem_op[1].sem_flg = SEM_UNDO|IPC_NOWAIT;
  if (semop(semid, sem_op, 2) == -1) {
#else
  sem_op[0].sem_flg = 0;
  if (semop(semid, sem_op, 1) == -1) {
#endif /* SAFE_SHM_READ */
    fprintf(stderr, "Fail to lock the semaphore\n");
    exit(1);
  }

  shmid =  get_grib_shmid();
  if (shmid == -1) {
    /* not there, we create it */
    fprintf(stderr, "Cannot find GRIB shared segment\n");
    exit(1);
  }

  segmaddr = get_grib_shmem(shmid, 1);
  construct_grib_array_from_shmem(&global_vlmc_context->windtable, segmaddr);

  time(&current_time);
  lat_boat     = degToRad(39.812);
  long_boat    = degToRad(8.43);
  for (i=0; i<4; current_time += 15*60, i++) {
    printf("Date: %s", ctime(&current_time));
    /* each 15mn */
    get_wind_info_latlong_UV(lat_boat, long_boat, 
			     current_time,
			     &wind_boat);
    printf("UV   Wind at  lat: %.2f long: %.2f, speed %.1f angle %.1f\n",
	   radToDeg(lat_boat), radToDeg(long_boat),
	   wind_boat.speed, radToDeg(wind_boat.angle));
    get_wind_info_latlong_TWSA(lat_boat, long_boat, 
			       current_time,
			       &wind_boat);
    printf("TWSA Wind at  lat: %.2f long: %.2f, speed %.1f angle %.1f\n",
	   radToDeg(lat_boat), radToDeg(long_boat),
	   wind_boat.speed, radToDeg(wind_boat.angle));
  }

  shmdt(segmaddr);
#ifdef SAFE_SHM_READ
  /* and release the semaphore */
  sem_op[0].sem_num = 0;
  sem_op[0].sem_op  = -1;
  sem_op[0].sem_flg = SEM_UNDO|IPC_NOWAIT;
  if (semop(semid, sem_op, 1) == -1) {
    fprintf(stderr, "Fail to unlock the semaphore\n");
    exit(1);
  }
#endif /* SAFE_SHM_READ */
  printf("\nWaypoint crossing:\n");
{'fraction_non_alphanumeric': 0.1619917549106782, 'fraction_numerical': 0.043488804462048336, 'mean_word_length': 3.585618977020015, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '3461357', 'n_tokens_mistral': 5345, 'n_tokens_neox': 4932, 'n_words': 1162}