<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <clutter/clutter.h>

#include "test-conform-common.h"

/* This test runs three timelines at 30 fps with 10 frames. Some of
   the timelines have markers. Once the timelines are run it then
   checks that all of the frames were hit, all of the markers were hit
   and that the completed signal was fired. The timelines are then run
   again but this time with a timeout source that introduces a
   delay. This should cause some frames to be skipped. The test is run
   again but only the markers and the completed signal is checked
   for. */

#define FRAME_COUNT 10
#define FPS         30

typedef struct _TimelineData TimelineData;

struct _TimelineData
{
  int timeline_num;

  guint frame_hit_count[FRAME_COUNT + 1];
  GSList *markers_hit;
  guint completed_count;
};

static void
timeline_data_init (TimelineData *data, int timeline_num)
{
  memset (data, 0, sizeof (TimelineData));
  data->timeline_num = timeline_num;
}

static void
timeline_data_destroy (TimelineData *data)
{
  g_slist_foreach (data->markers_hit, (GFunc) g_free, NULL);
  g_slist_free (data->markers_hit);
}

static void
timeline_complete_cb (ClutterTimeline *timeline,
                      TimelineData    *data)
{
  if (g_test_verbose ())
    g_print ("%i: Completed\n", data->timeline_num);

  data->completed_count++;
}

static void
timeline_new_frame_cb (ClutterTimeline *timeline,
                       gint             msec,
                       TimelineData    *data)
{
  /* Calculate an approximate frame number from the duration with
     rounding */
  int frame_no = ((msec * FRAME_COUNT + (FRAME_COUNT * 1000 / FPS) / 2)
                  / (FRAME_COUNT * 1000 / FPS));

  if (g_test_verbose ())
    g_print ("%i: Doing frame %d, delta = %i\n",
             data->timeline_num, frame_no,
             clutter_timeline_get_delta (timeline));

  g_assert (frame_no >= 0 && frame_no <= FRAME_COUNT);

  data->frame_hit_count[frame_no]++;
}

static void
timeline_marker_reached_cb (ClutterTimeline *timeline,
                            const gchar     *marker_name,
                            guint            frame_num,
                            TimelineData    *data)
{
  if (g_test_verbose ())
    g_print ("%i: Marker '%s' (%d) reached, delta = %i\n",
             data->timeline_num, marker_name, frame_num,
             clutter_timeline_get_delta (timeline));
  data->markers_hit = g_slist_prepend (data->markers_hit,
                                       g_strdup (marker_name));
}

static gboolean
check_timeline (ClutterTimeline *timeline,
                TimelineData    *data,
                gboolean         check_missed_frames)
{
  gchar **markers;
  gsize n_markers;
  guint *marker_reached_count;
  gboolean succeeded = TRUE;
  GSList *node;
  int i;
  int missed_frame_count = 0;
  int frame_offset;

  if (clutter_timeline_get_direction (timeline) == CLUTTER_TIMELINE_BACKWARD)
    frame_offset = 0;
  else
    frame_offset = 1;

  markers = clutter_timeline_list_markers (timeline, -1, &n_markers);
  marker_reached_count = g_new0 (guint, n_markers);

  for (node = data->markers_hit; node; node = node->next)
    {
      for (i = 0; i < n_markers; i++)
        if (!strcmp (node->data, markers[i]))
          break;

      if (i < n_markers)
        marker_reached_count[i]++;
      else
        {
          if (g_test_verbose ())
            g_print ("FAIL: unknown marker '%s' hit for timeline %i\n",
                     (char *) node->data, data->timeline_num);
          succeeded = FALSE;
        }
    }

  for (i = 0; i < n_markers; i++)
    if (marker_reached_count[i] != 1)
      {
        if (g_test_verbose ())
          g_print ("FAIL: marker '%s' hit %i times for timeline %i\n",
                   markers[i], marker_reached_count[i], data->timeline_num);
        succeeded = FALSE;
      }

  if (check_missed_frames)
    {
      for (i = 0; i < FRAME_COUNT; i++)
        if (data->frame_hit_count[i + frame_offset] < 1)
          missed_frame_count++;

      if (missed_frame_count)
        {
          if (g_test_verbose ())
            g_print ("FAIL: missed %i frame%s for timeline %i\n",
                     missed_frame_count, missed_frame_count == 1 ? "" : "s",
                     data->timeline_num);
          succeeded = FALSE;
        }
    }

  if (data->completed_count != 1)
    {
      if (g_test_verbose ())
        g_print ("FAIL: timeline %i completed %i times\n",
                 data->timeline_num, data->completed_count);
      succeeded = FALSE;
    }

  g_strfreev (markers);
  g_free (marker_reached_count);

  return succeeded;
}

static gboolean
timeout_cb (gpointer data G_GNUC_UNUSED)
{
  clutter_main_quit ();

  return FALSE;
}

static gboolean
delay_cb (gpointer data)
{
  /* Waste a bit of time so that it will skip frames */
  g_usleep (G_USEC_PER_SEC * 66 / 1000);

  return TRUE;
}

void
test_timeline (TestConformSimpleFixture *fixture,
	       gconstpointer data)
{
  ClutterActor *stage;
  ClutterTimeline *timeline_1;
  TimelineData data_1;
  ClutterTimeline *timeline_2;
  TimelineData data_2;
  ClutterTimeline *timeline_3;
  TimelineData data_3;
  gchar **markers;
  gsize n_markers;
  guint delay_tag;

  /* NB: We have to ensure a stage is instantiated else the master
   * clock wont run... */
  stage = clutter_stage_get_default ();

  timeline_data_init (&data_1, 1);
  timeline_1 = clutter_timeline_new (FRAME_COUNT * 1000 / FPS);
  clutter_timeline_add_marker_at_time (timeline_1, "start-marker",
                                       0 * 1000 / FPS);
  clutter_timeline_add_marker_at_time (timeline_1, "foo", 5 * 1000 / FPS);
  clutter_timeline_add_marker_at_time (timeline_1, "bar", 5 * 1000 / FPS);
  clutter_timeline_add_marker_at_time (timeline_1, "baz", 5 * 1000 / FPS);
  clutter_timeline_add_marker_at_time (timeline_1, "near-end-marker",
                                       9 * 1000 / FPS);
  clutter_timeline_add_marker_at_time (timeline_1, "end-marker",
                                       10 * 1000 / FPS);
  markers = clutter_timeline_list_markers (timeline_1, 5 * 1000 / FPS,
                                           &n_markers);
  g_assert (markers != NULL);
  g_assert (n_markers == 3);
  g_strfreev (markers);<commit_msg>test-timeline: Ignore the default stage<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <clutter/clutter.h>

#include "test-conform-common.h"

/* This test runs three timelines at 30 fps with 10 frames. Some of
   the timelines have markers. Once the timelines are run it then
   checks that all of the frames were hit, all of the markers were hit
   and that the completed signal was fired. The timelines are then run
   again but this time with a timeout source that introduces a
   delay. This should cause some frames to be skipped. The test is run
   again but only the markers and the completed signal is checked
   for. */

#define FRAME_COUNT 10
#define FPS         30

typedef struct _TimelineData TimelineData;

struct _TimelineData
{
  int timeline_num;

  guint frame_hit_count[FRAME_COUNT + 1];
  GSList *markers_hit;
  guint completed_count;
};

static void
timeline_data_init (TimelineData *data, int timeline_num)
{
  memset (data, 0, sizeof (TimelineData));
  data->timeline_num = timeline_num;
}

static void
timeline_data_destroy (TimelineData *data)
{
  g_slist_foreach (data->markers_hit, (GFunc) g_free, NULL);
  g_slist_free (data->markers_hit);
}

static void
timeline_complete_cb (ClutterTimeline *timeline,
                      TimelineData    *data)
{
  if (g_test_verbose ())
    g_print ("%i: Completed\n", data->timeline_num);

  data->completed_count++;
}

static void
timeline_new_frame_cb (ClutterTimeline *timeline,
                       gint             msec,
                       TimelineData    *data)
{
  /* Calculate an approximate frame number from the duration with
     rounding */
  int frame_no = ((msec * FRAME_COUNT + (FRAME_COUNT * 1000 / FPS) / 2)
                  / (FRAME_COUNT * 1000 / FPS));

  if (g_test_verbose ())
    g_print ("%i: Doing frame %d, delta = %i\n",
             data->timeline_num, frame_no,
             clutter_timeline_get_delta (timeline));

  g_assert (frame_no >= 0 && frame_no <= FRAME_COUNT);

  data->frame_hit_count[frame_no]++;
}

static void
timeline_marker_reached_cb (ClutterTimeline *timeline,
                            const gchar     *marker_name,
                            guint            frame_num,
                            TimelineData    *data)
{
  if (g_test_verbose ())
    g_print ("%i: Marker '%s' (%d) reached, delta = %i\n",
             data->timeline_num, marker_name, frame_num,
             clutter_timeline_get_delta (timeline));
  data->markers_hit = g_slist_prepend (data->markers_hit,
                                       g_strdup (marker_name));
}

static gboolean
check_timeline (ClutterTimeline *timeline,
                TimelineData    *data,
                gboolean         check_missed_frames)
{
  gchar **markers;
  gsize n_markers;
  guint *marker_reached_count;
  gboolean succeeded = TRUE;
  GSList *node;
  int i;
  int missed_frame_count = 0;
  int frame_offset;

  if (clutter_timeline_get_direction (timeline) == CLUTTER_TIMELINE_BACKWARD)
    frame_offset = 0;
  else
    frame_offset = 1;

  markers = clutter_timeline_list_markers (timeline, -1, &n_markers);
  marker_reached_count = g_new0 (guint, n_markers);

  for (node = data->markers_hit; node; node = node->next)
    {
      for (i = 0; i < n_markers; i++)
        if (!strcmp (node->data, markers[i]))
          break;

      if (i < n_markers)
        marker_reached_count[i]++;
      else
        {
          if (g_test_verbose ())
            g_print ("FAIL: unknown marker '%s' hit for timeline %i\n",
                     (char *) node->data, data->timeline_num);
          succeeded = FALSE;
        }
    }

  for (i = 0; i < n_markers; i++)
    if (marker_reached_count[i] != 1)
      {
        if (g_test_verbose ())
          g_print ("FAIL: marker '%s' hit %i times for timeline %i\n",
                   markers[i], marker_reached_count[i], data->timeline_num);
        succeeded = FALSE;
      }

  if (check_missed_frames)
    {
      for (i = 0; i < FRAME_COUNT; i++)
        if (data->frame_hit_count[i + frame_offset] < 1)
          missed_frame_count++;

      if (missed_frame_count)
        {
          if (g_test_verbose ())
            g_print ("FAIL: missed %i frame%s for timeline %i\n",
                     missed_frame_count, missed_frame_count == 1 ? "" : "s",
                     data->timeline_num);
          succeeded = FALSE;
        }
    }

  if (data->completed_count != 1)
    {
      if (g_test_verbose ())
        g_print ("FAIL: timeline %i completed %i times\n",
                 data->timeline_num, data->completed_count);
      succeeded = FALSE;
    }

  g_strfreev (markers);
  g_free (marker_reached_count);

  return succeeded;
}

static gboolean
timeout_cb (gpointer data G_GNUC_UNUSED)
{
  clutter_main_quit ();

  return FALSE;
}

static gboolean
delay_cb (gpointer data)
{
  /* Waste a bit of time so that it will skip frames */
  g_usleep (G_USEC_PER_SEC * 66 / 1000);

  return TRUE;
}

void
test_timeline (TestConformSimpleFixture *fixture,
	       gconstpointer data)
{
  ClutterActor *stage G_GNUC_UNUSED;
  ClutterTimeline *timeline_1;
  TimelineData data_1;
  ClutterTimeline *timeline_2;
  TimelineData data_2;
  ClutterTimeline *timeline_3;
  TimelineData data_3;
  gchar **markers;
  gsize n_markers;
  guint delay_tag;

  /* NB: We have to ensure a stage is instantiated else the master
   * clock wont run... */
  stage = clutter_stage_get_default ();

  timeline_data_init (&data_1, 1);
  timeline_1 = clutter_timeline_new (FRAME_COUNT * 1000 / FPS);
  clutter_timeline_add_marker_at_time (timeline_1, "start-marker",
                                       0 * 1000 / FPS);
  clutter_timeline_add_marker_at_time (timeline_1, "foo", 5 * 1000 / FPS);
  clutter_timeline_add_marker_at_time (timeline_1, "bar", 5 * 1000 / FPS);
  clutter_timeline_add_marker_at_time (timeline_1, "baz", 5 * 1000 / FPS);
  clutter_timeline_add_marker_at_time (timeline_1, "near-end-marker",
                                       9 * 1000 / FPS);
  clutter_timeline_add_marker_at_time (timeline_1, "end-marker",
                                       10 * 1000 / FPS);
  markers = clutter_timeline_list_markers (timeline_1, 5 * 1000 / FPS,
                                           &n_markers);
  g_assert (markers != NULL);
  g_assert (n_markers == 3);
  g_strfreev (markers);
{'fraction_non_alphanumeric': 0.09919444005686305, 'fraction_numerical': 0.01500552835255094, 'mean_word_length': 2.5009676527508984, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7532158', 'n_tokens_mistral': 4467, 'n_tokens_neox': 4194, 'n_words': 1072}